Java完成邮件发送,保姆级教学(QQ邮箱为例)
1.开启QQ邮箱第三方服务(我的已经开启了)
继续按照要求操作、获取到授权码
2.导入依赖
<!--mail邮件发送-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<!--redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
3.编写配置文件
spring:
# mail邮箱
mail:
# SMTP服务器(我用的是QQ邮箱的SMTP服务器地址,如果用的其它邮箱请另行百度搜索)
host: smtp.qq.com
port: 465
# 发送验证码的邮箱(发件人的邮箱)
username: ***********@qq.com
# 授权码
password: **************
# 编码
default-encoding: utf-8
# 其它参数
properties:
mail:
smtp:
# 如果是用SSL方式,需要配置如下属性,使用qq邮箱的话需要开启
ssl:
enable: true
required: true
# 邮件接收时间的限制,单位毫秒
timeout: 10000
# 连接时间的限制,单位毫秒
connection-timeout: 10000
# 邮件发送时间的限制,单位毫秒
write-timeout: 10000
redis:
host: 127.0.0.1 # 主机
port: 6379 # 端口号
jedis:
pool:
max-active: 8 #最大活跃的连接数,最多几个连接
max-idle: 8 #最多空闲连接数
max-wait: -1ms #最大等待时间 -1一直等待
min-idle: 0 #最小空闲数
4.编写生成验证码及校验验证码的工具类
/**
* 验证码相关工具类
*/
@Component
@Slf4j
public class VerifyCodeUtil {
@Resource
private StringRedisTemplate stringRedisTemplate;
/**
* 随机生成指定长度字符串验证码
*
* @param length 验证码长度
*/
public String generateVerifyCode(int length) {
String strRange = "1234567890";
StringBuilder strBuilder = new StringBuilder();
for (int i = 0; i < length; ++i) {
char ch = strRange.charAt((new Random()).nextInt(strRange.length()));
strBuilder.append(ch);
}
return strBuilder.toString();
}
/**
* 校验验证码(可用作帐号登录、注册、修改信息等业务)
* 思路:先检查redis中是否有key位对应email的键值对,没有代表验证码过期;如果有就判断用户输入的验证码与value是否相同,进而判断验证码是否正确。
*/
public Integer checkVerifyCode(String email, String code) {
int result = 1;
ValueOperations<String, String> valueOperations = stringRedisTemplate.opsForValue();
String msgKey = "msg_" + email;
String verifyCode = valueOperations.get(msgKey);
/*校验验证码:0验证码错误、1验证码正确、2验证码过期*/
if (verifyCode == null) {
result = 2;
} else if (!code.equals(verifyCode)) {
result = 0;
}
// 如果验证码正确,则从redis删除
if (result == 1) {
stringRedisTemplate.delete(msgKey);
}
return result;
}
}
5.编写发送邮箱验证码接口
①controller层
/**
* @Author: one piece
* @Description: 邮箱验证码
* @Date: 2023/11/12 11:31
*/
@RestController
@RequestMapping("/mail")
@Slf4j
public class MailController {
@Resource
private MailService mailService;
@PostMapping("/sendMail")
public String sendMail(@RequestBody String email , HttpServletRequest request) throws UnsupportedEncodingException {
log.debug(email);
//由于编码问题会把@符号解析为@40所以进行如下处理
String encodedEmail = request.getParameter("email");
String decodedEmail = URLDecoder.decode(encodedEmail, "UTF-8");
log.info(decodedEmail);
String message = mailService.sendEmail(decodedEmail);
return message;
}
②Service层
/**
* @Author: one piece
* @Description:
* @Date: 2024/2/13 14:21
*/
@Service
@Slf4j
public class MailService {
@Resource
private VerifyCodeUtil verifyCodeUtil;
@Resource
private JavaMailSender javaMailSender;
@Resource
private StringRedisTemplate stringRedisTemplate;
public String sendEmail(String email) {
if (email.isEmpty()){
return "邮箱不能为空";
}
// 定义Redis的key
String key = "msg_" + email;
ValueOperations<String, String> valueOperations = stringRedisTemplate.opsForValue();
String verifyCode = valueOperations.get(key);
if (verifyCode == null) {
// 随机生成一个6位数字型的字符串
String code = verifyCodeUtil.generateVerifyCode(6);
// 邮件对象(邮件模板,根据自身业务修改)
SimpleMailMessage message = new SimpleMailMessage();
message.setSubject("验证码");
message.setText("尊敬的用户您好!\n\n感谢您的关注。\n\n尊敬的: " + email + "您的校验验证码为: " + code + ",有效期5分钟,请不要把验证码信息泄露给其他人,如非本人请勿操作");
message.setTo(email);
try {
// 对方看到的发送人(发件人的邮箱,根据实际业务进行修改,一般填写的是企业邮箱,*号位置写发件者邮箱)
message.setFrom(new InternetAddress(MimeUtility.encodeText("one piece") + "<**********@qq.com>").toString());
// 发送邮件
javaMailSender.send(message);
// 将生成的验证码存入Redis数据库中,并设置过期时间
valueOperations.set(key, code, 5, TimeUnit.MINUTES);
log.debug(message.toString());
return "验证码已发送至您的邮箱,请注意查收";
} catch (Exception e) {
return "发送失败";
}
} else {
return "验证码已发送至您的邮箱,请注意查收";
}
}
如果对大家有帮助请点个赞、点个关注!!!