springBoot 集成 hutool邮箱发送功能

pom文件导入依赖

1
2
3
4
5
6
<!--- 邮箱 -->
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>

在resource文件夹下创建config目录,config目录下创建mail.setting文件

文件如下

1
2
3
from = 熹烨小说分析系统<x.xxxx@qq.com>
pass = #####邮件服务器密码
sslEnable = true

在service中使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public Result send(SendEmailVo sendEmailVo) {
String publickey = sendEmailVo.getKey();
String privatekey = stringRedisTemplate.opsForValue().get(publickey);
if (StrUtil.isBlank(privatekey)) {
return Result.error("406", "注册时间过长,请刷新页面");
}
String toemail = Sm2.doDecrypt(sendEmailVo.getEmail(), privatekey);
stringRedisTemplate.delete(publickey);

String rellycode = RandomUtil.randomString(6);
MailUtil.send(toemail,"邮箱验证" , "您的验证码是:<h1>" + rellycode + "</h1>,有效时间为 <h3>5分钟</h3>", true);
stringRedisTemplate.opsForValue().set(toemail, rellycode,5,TimeUnit.MINUTES);
return Result.success("发送邮件成功,请进入邮箱查看",null);
}