# 功能描述
实现对邮件的处理,包括收发邮件等。
配置文件为application-email.yml
提示
(1) 需在application.yml中增加spring.profiles.active中增加email才能激活配置文件
(2) 需设置application-email.yml中的spring.mail.enabled为true才能开启email服务
# 配置一览表
spring:
mail:
enabled: true #设置为true才开启email服务
host: smtp.qq.com #发送邮件服务器
username: 1531753904@qq.com #QQ邮箱
password: #客户端授权码
protocol: smtp #发送邮件协议
properties.mail.smtp.auth: true
properties.mail.smtp.port: 465 #端口号465或587
properties.mail.display.sendmail: Javen #可以任意
properties.mail.display.sendname: Spring Boot Guide Email #可以任意
properties.mail.smtp.starttls.enable: true
properties.mail.smtp.starttls.required: true
properties.mail.smtp.ssl.enable: true
default-encoding: utf-8
from: 1531753904@qq.com #与上面的username保持一致
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# Service接口
# 第一步 引入缓存包
<dependency>
<groupId>sei-cloud</groupId>
<artifactId>email</artifactId>
</dependency>
1
2
3
4
2
3
4
# 第二步 引入接口
@Resource
EmailService emailService;
1
2
2
# 第三步 使用接口
/**
* 邮件接口
* @author xiong
*/
public interface EmailService {
/**
* 发送文本邮件
* @param to: 收件人
* @param subject: 主题
* @param content: 内容
*/
void sendSimpleMail(@NonNull String to, @NonNull String subject, @Nullable String content);
/**
* 发送文本邮件
* @param to: 收件人
* @param subject: 主题
* @param content: 内容
* @param cc: 抄送
*/
void sendSimpleMail(@NonNull String to, @NonNull String subject,@Nullable String content, @Nullable String... cc);
/**
* 发送正文中有静态资源和附件的html邮件
* @param isHtml: html邮件为true,文本为false
* @param to: 收件人
* @param subject: 主题
* @param content: 内容
* @param inFile: 邮件正文中嵌入的内容
* @param attFile: 邮件附件
* @param cc: 抄送
* @throws MessagingException 异常
*/
void sendMail(@NonNull boolean isHtml, @NonNull String to, @NonNull String subject, @NonNull String content, @Nullable String[] inFile, @Nullable String[] attFile, @Nullable String... cc) throws MessagingException;
/**
* 发送html邮件
* @param to: 收件人
* @param subject: 主题
* @param content: 内容
* @throws MessagingException 异常
*/
void sendHtmlMail(@NonNull String to, @NonNull String subject, @NonNull String content) throws MessagingException;
/**
* 获得邮件模板内容
* @param templatePathFileName: 模块文件路径及名称
* @param context: 内容
* @return String
*/
String getTemplate(@NonNull String templatePathFileName, @NonNull Context context);
}
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
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