pom依赖
<!--邮箱-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
EmailController 代码
package com.ruoyi.web.controller.email;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.constant.EmailConstants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.control.dto.EmailEntityDTO;
import com.ruoyi.control.utils.EmailUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.Random;
import java.util.concurrent.TimeUnit;
@RestController
@RequestMapping("/system/email")
public class EmailController {
@Resource
EmailUtils emailUtils;
@Autowired
private RedisCache redisCache;
// /system/email/sendEmail
@RepeatSubmit()
@PostMapping("/sendEmail")
public AjaxResult verifyEmail(@RequestBody EmailEntityDTO emailEntity) {
if(StringUtils.isBlank( emailEntity.getSendTo() ) ){
return AjaxResult.error("收件人不能为空");
}
Random random = new Random();
int randomNumber = random.nextInt(10000);
while (randomNumber < 1000) {
randomNumber = random.nextInt(10000);
}
String code = String.valueOf(randomNumber);
String verifyKey = EmailConstants.Email_CODE + emailEntity.getSendTo();
redisCache.setCacheObject(verifyKey, code , Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
String detail = "您的邮件验证码为:" + code + "用于绑定邮箱身份验证,2分钟内有效,请勿泄露和转发。";
emailEntity.setDetail(detail);
emailEntity.setTitle( "验证码" );
return emailUtils.sendSimpleMail(emailEntity);
}
}
EmailUtils 发送工具
package com.ruoyi.control.utils;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.control.dto.EmailEntityDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
@Service
public class EmailUtils {
@Value("${spring.mail.username}")
private String username;
@Autowired
JavaMailSender mailSender;
public AjaxResult sendSimpleMail(EmailEntityDTO emailEntity) {
try {
MimeMessage mailMessage=mailSender.createMimeMessage();
MimeMessageHelper helper=new MimeMessageHelper(mailMessage,true, Constants.UTF_8);
//设置寄件人
helper.setFrom(username);
//设置收件人
helper.setTo(emailEntity.getSendTo());
//设置邮件标题
helper.setSubject(emailEntity.getTitle());
//设置邮件内容
helper.setText(emailEntity.getDetail());
mailSender.send(mailMessage);
} catch (MessagingException e) {
e.printStackTrace();
}
return AjaxResult.success("邮件发送成功");
}
}
常量
public class EmailConstants {
public static final String Email_CODE = "emailCode:";
}
DTO
package com.ruoyi.control.dto;
import lombok.Data;
@Data
public class EmailEntityDTO {
private String sendTo; //收件邮箱
private String title; //邮箱标题
private String detail; //邮箱内容
}
配置文件
# Spring配置
spring:
mail: #邮箱 163 邮箱
default-encoding: UTF-8
host: smtp.163.com
username: lo...k...@163.com
password: HMMCWMHGR
邮箱配置
需要获取验证,验证后能获取授权密码
企业端163
灵犀办公免费版-邮件(网易企业邮箱) url: https://mailh.qiye.163.com/
点击开启按钮,获取授权码
配置信息
default-encoding: UTF-8
host: smtphz.qiye.163.com
username: 自己的邮箱地址
password: 获取的授权码
spring:
mail: #邮箱
default-encoding: UTF-8
host: smtphz.qiye.163.com
username: .......esmail.com
password: yd6K..........