1 前言
邮箱验证码在项目用户注册模块中经常出现,虽然现在有了短信验证码,但是我们可能无法承担短信云服务的费用,所以邮箱验证码还是比较实用的。下面就来介绍SpringBoot中如何集成邮箱验证码功能!
2 实现
2.1 导入依赖
导入邮箱依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
2.2 配置邮箱信息
注意:需要开启邮箱的pop3/smtp服务
spring:
mail:
host: smtp.163.com
# 发送邮件的邮箱账号
username: 111@qq.com
# 密钥,不是邮箱密码
password: xxxxxx
若要将项目部署到服务器就不能使用上述的配置,需要使用下方配置。原因是各大云服务器厂商对25端口进行了限制,防止垃圾邮件的发送。
spring:
mail:
default-encoding: UTF-8
host: smtp.163.com
# 密钥
password: xxxx
port: 465
# 邮箱
username: 111@qq.com
properties:
mail:
smtp:
auth: true
socketFactory:
class: javax.net.ssl.SSLSocketFactory
starttls:
enable: true
2.3 创建邮件模板文件
在resources目录下创建邮件模板mailtemplate.ftl文件。直接复制即可,对样式有需求可自行修改。
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="description" content="email code">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<!--邮箱验证码模板-->
<body>
<div style="background-color:#ECECEC; padding: 10px;">
<table cellpadding="0" align="center"
style="width: 100%;height: 100%; margin: 0px auto; text-align: left; position: relative; border-top-left-radius: 5px; border-top-right-radius: 5px; border-bottom-right-radius: 5px; border-bottom-left-radius: 5px; font-size: 14px; font-family:微软雅黑, 黑体; line-height: 1.5; box-shadow: rgb(153, 153, 153) 0px 0px 5px; border-collapse: collapse; background-position: initial initial; background-repeat: initial initial;background:#fff;">
<tbody>
<tr>
<th valign="middle"
style="height: 25px; line-height: 25px; padding: 15px 35px; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: RGB(148,0,211); background-color: RGB(148,0,211); border-top-left-radius: 5px; border-top-right-radius: 5px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px;">
<font face="微软雅黑" size="5" style="color: rgb(255, 255, 255); ">Anime验证</font>
</th>
</tr>
<tr>
<td style="word-break:break-all">
<div style="padding:25px 35px 40px; background-color:#fff;opacity:0.8;">
<h2 style="margin: 5px 0px; ">
<font color="#333333" style="line-height: 20px; ">
<font style="line-height: 22px; " size="4">
尊敬的用户:</font>
</font>
</h2>
<p>您好!感谢您使用Anime,您的账号正在进行邮箱验证,验证码为:<span style="color: #ff0000; font-size: 20px;">{0}</span>,有效期5分钟,请尽快填写验证码完成验证!
</p><br>
<div style="width:100%;margin:0 auto;">
<div style="padding:10px 10px 0;border-top:1px solid #ccc;color:#747474;margin-bottom:20px;line-height:1.3em;font-size:12px;">
<p>****团队</p>
<p>联系我们:********</p>
<br>
<p>此为系统邮件,请勿回复<br>
Please do not reply to this system email
</p>
<!--<p>©***</p>-->
</div>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
2.4 创建邮件内容构建类
package com.example.animeApi.utils;
/**
* 通过mailtemplate.ftl文件构建邮件内容
*/
@Slf4j
public class BuildEmailContentUtils {
private BuildEmailContentUtils() {}
public static String getContent(String code) {
//加载邮件html模板
Resource resource = new ClassPathResource("mailtemplate.ftl");
InputStream inputStream = null;
BufferedReader fileReader = null;
StringBuffer buffer = new StringBuffer();
String line = "";
try {
inputStream = resource.getInputStream();
fileReader = new BufferedReader(new InputStreamReader(inputStream));
while ((line = fileReader.readLine()) != null) {
buffer.append(line);
}
} catch (Exception e) {
log.info("发送邮件读取模板失败{}", e);
} finally {
if (fileReader != null) {
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//替换html模板中的参数
return MessageFormat.format(buffer.toString(), code);
}
}
2.5 发送邮件
package com.example;
@SpringBootTest
class SpringBoot01DemoApplicationTests {
@Autowired
private JavaMailSender sender;
@Test
void contextLoads() throws MessagingException {
MimeMessage message = sender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
//邮件主题
helper.setSubject("Anime");
//邮件内容,通过上面邮件内容构建类来生成
helper.setText(BuildEmailContentUtils.getContent("1234"), true);
//发送给email
helper.setTo(to@qq.com);
//发送的邮箱,此邮箱要与配置文件中的邮箱一致
helper.setFrom(from@qq.com);
sender.send(message);
}
}