SpringBoot邮件发送(qq邮箱)

1、开启POP3|SMTP服务

首先登录QQ邮箱>>>登录成功后找到设置>>>然后找到邮箱设置>>>点击账户>>>找到POP3|SMTP服务>>>点击开启(开启需要验证,验证成功后会有一串授权码用于发送邮件使用)>>>验证成功
记下QQ邮箱提示的授权码,这个授权码,就是发送邮件时需要的密码。

2、引入依赖

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>

3、application.yml配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
spring:
#邮箱基本配置
mail:
#配置smtp服务主机地址
# qq邮箱为smtp.qq.com 端口号465或587
# sina smtp.sina.cn
# aliyun smtp.aliyun.com
# 163 smtp.163.com 端口号465或994
host: smtp.qq.com
#发送者邮箱
username: ****@qq.com
#配置密码,注意不是真正的密码,而是刚刚申请到的授权码
password: fe456156fefefee
#端口号465或587
port: 465
#默认的邮件编码为UTF-8
default-encoding: UTF-8
#其他参数
properties:
mail:
#配置SSL 加密工厂
smtp:
ssl:
#本地测试,先放开ssl
enable: true
#开启debug模式,这样邮件发送过程的日志会在控制台打印出来,方便排查错误
debug: true

4、编写发送邮件的方法

邮件生成工具类 EmailUtil.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/**
* @Author: Ohh
* @Desctription: 电子邮件生成内容实用程序
* @Date: 2024-05-18 11:52
* @Version: 1.0
*/
@Slf4j
public class EmailUtil {

/**
* 生成电子邮件内容
*
* @param captcha 验证码
* @param emailHtmlPath 电子邮件html路径
* @return {@link String}
*/
public static String buildEmailContent(String emailHtmlPath, String captcha) {
// 加载邮件html模板
ClassPathResource resource = new ClassPathResource(emailHtmlPath);
InputStream inputStream = null;
BufferedReader fileReader = null;
StringBuilder buffer = new StringBuilder();
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.getMessage());
} 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(), captcha, EMAIL_TITLE, EMAIL_TITLE_ENGLISH, PLATFORM_RESPONSIBLE_PERSON, PLATFORM_ADDRESS);
}

/**
* 构建付费成功电子邮件内容
*
* @param emailHtmlPath 电子邮件html路径
* @param orderName 订单名称
* @param orderTotal 订单总额
* @return {@link String}
*/
public static String buildPaySuccessEmailContent(String emailHtmlPath, String orderName, String orderTotal) {
// 加载邮件html模板
ClassPathResource resource = new ClassPathResource(emailHtmlPath);
InputStream inputStream = null;
BufferedReader fileReader = null;
StringBuilder buffer = new StringBuilder();
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.getMessage());
} 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(), orderName, orderTotal, PLATFORM_RESPONSIBLE_PERSON, PATH_ADDRESS, EMAIL_TITLE);
}

/**
* 发送支付成功电子邮件
*
* @param emailAccount 电子邮件帐户
* @param mailSender 邮件发件人
* @param emailConfig 电子邮件配置
* @param orderName 订单名称
* @param orderTotal 订单总额
* @throws MessagingException 消息传递异常
*/
public void sendPaySuccessEmail(String emailAccount, JavaMailSender mailSender, EmailConfig emailConfig, String orderName, String orderTotal) throws MessagingException {
MimeMessage message = mailSender.createMimeMessage();
// 邮箱发送内容组成
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setSubject("【" + EMAIL_TITLE + "】感谢您的购买,请查收您的订单");
helper.setText(buildPaySuccessEmailContent(EMAIL_HTML_PAY_SUCCESS_PATH, orderName, orderTotal), true);
helper.setTo(emailAccount);
helper.setFrom(EMAIL_TITLE + '<' + emailConfig.getEmailFrom() + '>');
mailSender.send(message);
}

}

邮件常量 EmailConstant.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
* @Author: Ohh
* @Desctription: 电子邮件常量
* @Date: 2024-05-17 23:20
* @Version: 1.0
*/
public interface EmailConstant {
/**
* 电子邮件html内容路径 resources目录下
*/
String EMAIL_HTML_CONTENT_PATH = "email.html";

/**
* 电子邮件html支付成功路径
*/
String EMAIL_HTML_PAY_SUCCESS_PATH = "pay.html";

/**
* captcha缓存键
*/
String CAPTCHA_CACHE_KEY = "api:captcha:";

/**
* 电子邮件主题
*/
String EMAIL_SUBJECT = "验证码邮件";

/**
* 电子邮件标题
*/
String EMAIL_TITLE = "LinkSauce-API 接口开放平台";

/**
* 电子邮件标题英语
*/
String EMAIL_TITLE_ENGLISH = "LinkSauce-API Open Interface Platform";

/**
* 平台负责人
*/
String PLATFORM_RESPONSIBLE_PERSON = "Ohh";

/**
* todo 平台地址
*/
String PLATFORM_ADDRESS = "<a href='https://api.qimuu.icu/'>请联系我们</a>";

String PATH_ADDRESS = "'https://api.qimuu.icu/'";
}

邮件配置 EmailConfig.java

1
2
3
4
5
6
7
8
9
10
11
12
13
/**
* @Author: Ohh
* @Desctription: 电子邮件配置
* @Date: 2024-05-18 11:55
* @Version: 1.0
*/
@Configuration
@ConfigurationProperties(prefix = "spring.mail")
@Data
public class EmailConfig {
private String emailFrom = "*****@qq.com";
}

发送邮件方法 sendEmail

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
private void sendEmail(String emailAccount, String captcha) throws MessagingException {
// 创建MimeMessage对象
MimeMessage message = mailSender.createMimeMessage();
// 邮箱发送内容组成
MimeMessageHelper helper = new MimeMessageHelper(message, true);
// 设置主题
helper.setSubject(EMAIL_SUBJECT);
// 设置正文
helper.setText(buildEmailContent(EMAIL_HTML_CONTENT_PATH, captcha), true);
// 设置收件人
helper.setTo(emailAccount);
// 设置发件人
helper.setFrom(EMAIL_TITLE + '<' + emailConfig.getEmailFrom() + '>');
// 发送邮件
mailSender.send(message);
}

邮件样式模板 email.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta content="IE=edge" http-equiv="X-UA-Compatible">
<meta content="email code" name="description">
<meta content="width=device-width, initial-scale=1" name="viewport">
</head>
<!--邮箱验证码模板-->
<body>
<div style="background-color:#ECECEC; padding: 35px;">
<table align="center" cellpadding="0"
style="width: 600px;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 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;"
valign="middle">
<font face="微软雅黑" size="5" style="color: rgb(255, 255, 255); ">{1}</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 size="4" style="line-height: 22px; ">
尊敬的用户:</font>
</font>
</h2>
<!-- 中文 -->
<p>您好!感谢您使用{1},您的账号正在进行邮箱验证,验证码为:<span style="color: '#ff8c00';font-size: 16px;font-weight: bold">{0}</span>
,有效期5分钟,请尽快填写验证码完成验证!</p><br>
<!-- 英文 -->
<h2 style="margin: 5px 0px; ">
<font color="#333333" style="line-height: 20px; ">
<font size="4" style="line-height: 22px; ">
Dear user:</font>
</font>
</h2>
<p>Hello! Thanks for using {2}, your account is being authenticated by email, the
verification code is: <span
style="color: '#ff8c00';font-size: 16px;font-weight: bold"> {0} </span> , valid for 5
minutes. Please fill in the
verification code as soon as
possible!</p>
<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>{3}</p>
<p>此电子邮件仅限本人查看!如果有人要求你与他分享此 电子邮件或验证,或你认为误收此电子邮件,{4}</p>
<br>
<p>此为系统邮件,请勿回复<br>
Please do not reply to this system email
</p>
</div>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>