springboot2 实现邮件发送功能,QQ/Gmail/163/126..
效果图如下:
技术选型
- Spring Boot 2.1.3.RELEASE (原本官网推荐 2.1.5.RELEASE,可是搭建途中发现部分注解未生效,故改之)
- Thymeleaf (用作邮件模板)
- JDK 1.8
简要讲解
依赖以及配置
这里还是用的 Spring Boot 来整合,自带了模块依赖1
2
3
4<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
随着 Spring Boot 热度越来越大,现在从事 Java 的要是不知道 Spring Boot 的存在那就真的很不应该了。本来只是为了取代繁琐的 EJB,一直发展到了如今无所不在的地步。
然后在配置文件中进行对应的配置1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18server:
port: 8887
spring:
mail:
host: smtp.163.com
username: ffj0721@163.com
password: xxxx # 授权码
protocol: smtp
properties.mail.smtp.auth: true
properties.mail.smtp.port: 994
properties.mail.display.sendmail: DoubleFJ
properties.mail.display.sendname: Spring Boot Email
properties.mail.smtp.starttls.enable: true
properties.mail.smtp.starttls.required: true
properties.mail.smtp.ssl.enable: true
default-encoding: utf-8
from: ffj0721@163.com
一切就是这样的简单清晰。不同的邮件个别配置数据不同,请自行查阅,这里只用 163 做测试。
配置了之后我们开始操刀敲代码,其实只需要调用 JavaMailSender 接口即可,传参实现,已经给我们封装好了。
常用邮件接口
这里是几个常用的邮件接口: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
54package com.example.springbootmail.service;
import javax.mail.MessagingException;
/**
* 常用邮件接口
*/
public interface IMailService {
/**
* 发送文本邮件
* @param to
* @param subject
* @param content
*/
public void sendSimpleMail(String to, String subject, String content);
public void sendSimpleMail(String to, String subject, String content, String... cc);
/**
* 发送HTML邮件
* @param to
* @param subject
* @param content
* @throws MessagingException
*/
public void sendHtmlMail(String to, String subject, String content) throws MessagingException;
public void sendHtmlMail(String to, String subject, String content, String... cc);
/**
* 发送带附件的邮件
* @param to
* @param subject
* @param content
* @param filePath
* @throws MessagingException
*/
public void sendAttachmentsMail(String to, String subject, String content, String filePath) throws MessagingException;
public void sendAttachmentsMail(String to, String subject, String content, String filePath, String... cc);
/**
* 发送正文中有静态资源的邮件
* @param to
* @param subject
* @param content
* @param rscPath
* @param rscId
* @throws MessagingException
*/
public void sendResourceMail(String to, String subject, String content, String rscPath, String rscId) throws MessagingException;
public void sendResourceMail(String to, String subject, String content, String rscPath, String rscId, String... cc);
}
接口实现类注入 JavaMailSender
实现类分别实现上述接口 注入 JavaMailSender1
2
3
4
5
6
7
8
9
10
11/**
* 发送邮件实现类
*/
@Service
public class IMailServiceImpl implements IMailService {
@Autowired
private JavaMailSender mailSender;
@Value("${spring.mail.from}")
private String from;
其中 from
就是配置中我们配置的发送方,直接读取使用。
实现发送文本邮件
1 | /** |
如上所示,三个参数的,第一个是你要发邮件的对象,第二个是邮件标题,第三个是邮件发送的内容,第二个 cc 字符串数组就是需要抄送的对象。
实现发送 HTML 邮件
1 | /** |
官网 MimeMessageHelper 使用介绍。
实现发送带附件邮件
1 | /** |
这里附件名的截取要按照自己的实际需求来,有人喜欢用 \\
,有人喜欢用 /
,看具体情况具体分析了。
实现发送正文中有静态资源邮件
1 | /** |
其中 rscId 是资源的唯一 id,rscPath 就是对应资源的路径。具体待会我们看 Controller 调用方法。
实现发送模板邮件
前面说了我们选择使用 Thymeleaf
作为邮件的模板,那就需要在 POM 文件中加入对应依赖。1
2
3
4<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
然后在 templates 文件夹下新建 mailTemplate.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<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>spring-boot-mail test</title>
<style>
body {
text-align: center;
margin-left: auto;
margin-right: auto;
}
#welcome {
text-align: center;
}
</style>
</head>
<body>
<div id="welcome">
<h3>Welcome To My Friend!</h3>
GitHub:
<a href="#" th:href="@{${github_url}}" target="_bank">
<strong>GitHub</strong>
</a>
<br />
<br />
个人博客:
<a href="#" th:href="@{${blog_url}}" target="_bank">
<strong>DoubleFJ の Blog</strong>
</a>
<br />
<br />
<img width="258px" height="258px"
src="https://raw.githubusercontent.com/Folgerjun/materials/master/blog/img/WC-GZH.jpg">
<br />微信公众号(诗词鉴赏)
</div>
</body>
</html>
这个模板自己 DIY 即可,不过对应填充参数不可弄错。例如我上面的是 github_url
和 blog_url
。
调用实现
MailController.java1
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
109package com.example.springbootmail.controller;
import com.example.springbootmail.service.impl.IMailServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
@RestController
@RequestMapping("/Mail")
public class MailController {
private static final String SUCC_MAIL = "邮件发送成功!";
private static final String FAIL_MAIL = "邮件发送失败!";
// 图片路径
private static final String IMG_PATH = "C:/Users/zjj/Desktop/github/materials/blog/img/WC-GZH.jpg";
// 发送对象
private static final String MAIL_TO = "folgerjun@gmail.com";
@Autowired
private IMailServiceImpl mailService;
@Autowired
private TemplateEngine templateEngine;
@RequestMapping("/Email")
public String index(){
try {
mailService.sendSimpleMail(MAIL_TO,"这是一封普通的邮件","这是一封普通的SpringBoot测试邮件");
}catch (Exception ex){
ex.printStackTrace();
return FAIL_MAIL;
}
return SUCC_MAIL;
}
@RequestMapping("/htmlEmail")
public String htmlEmail(){
try {
mailService.sendHtmlMail(MAIL_TO,"这是一HTML的邮件","<body>\n" +
"<div id=\"welcome\">\n" +
" <h3>Welcome To My Friend!</h3>\n" +
"\n" +
" GitHub:\n" +
" <a href=\"#\" th:href=\"@{${github_url}}\" target=\"_bank\">\n" +
" <strong>GitHub</strong>\n" +
" </a>\n" +
" <br />\n" +
" <br />\n" +
" 个人博客:\n" +
" <a href=\"#\" th:href=\"@{${blog_url}}\" target=\"_bank\">\n" +
" <strong>DoubleFJ の Blog</strong>\n" +
" </a>\n" +
" <br />\n" +
" <br />\n" +
" <img width=\"258px\" height=\"258px\"\n" +
" src=\"https://raw.githubusercontent.com/Folgerjun/materials/master/blog/img/WC-GZH.jpg\">\n" +
" <br />微信公众号(诗词鉴赏)\n" +
"</div>\n" +
"</body>");
}catch (Exception ex){
ex.printStackTrace();
return FAIL_MAIL;
}
return SUCC_MAIL;
}
@RequestMapping("/attachmentsMail")
public String attachmentsMail(){
try {
mailService.sendAttachmentsMail(MAIL_TO, "这是一封带附件的邮件", "邮件中有附件,请注意查收!", IMG_PATH);
}catch (Exception ex){
ex.printStackTrace();
return FAIL_MAIL;
}
return SUCC_MAIL;
}
@RequestMapping("/resourceMail")
public String resourceMail(){
try {
String rscId = "DoubleFJ";
String content = "<html><body>这是有图片的邮件<br/><img src=\'cid:" + rscId + "\' ></body></html>";
mailService.sendResourceMail(MAIL_TO, "这邮件中含有图片", content, IMG_PATH, rscId);
}catch (Exception ex){
ex.printStackTrace();
return FAIL_MAIL;
}
return SUCC_MAIL;
}
@RequestMapping("/templateMail")
public String templateMail(){
try {
Context context = new Context();
context.setVariable("github_url", "https://github.com/Folgerjun");
context.setVariable("blog_url", "http://putop.top/");
String emailContent = templateEngine.process("mailTemplate", context);
mailService.sendHtmlMail(MAIL_TO, "这是模板邮件", emailContent);
}catch (Exception ex){
ex.printStackTrace();
return FAIL_MAIL;
}
return SUCC_MAIL;
}
}
程序运行后若访问 http://localhost:8887/Mail/Email 页面出现邮件发送成功!
字样就说明邮件发送已经实现了。