一、集成邮件服务
1.1 获取客户端授权码
1.2 引入依赖
<dependencies>
......
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
</dependencies>
1.3 设置邮件服务属性
spring:
# 邮箱相关配置
mail:
# 邮箱服务器
host: smtp.163.com
# 邮箱帐号
username: marklogzhu@163.com
# 邮箱密码
paswoord: 之前获取到的授权码
# 默认编码格式
default-encoding: utf-8
# 属性配置
properties:
mail:
smtp:
auth: true
starttls:
enable: tue
required: true
1.4 实现邮件服务
邮件服务接口
public interface MailService {
/**
* 发送简单邮件
*
* @param deliveryAddress 收件人地址
* @param subject 主题
* @param text 正文
*/
void sendSimpleMail(String deliveryAddress, String subject, String text);
/**
* 发送带附件邮件
*
* @param deliveryAddress 收件人地址
* @param subject 主题
* @param text 正文
* @param files 附件数组
* @throws MessagingException
*/
void sendAttachmentsMail(String deliveryAddress, String subject, String text, File[] files) throws MessagingException;
/**
* 发送带内联邮件
*
* @param deliveryAddress 收件人地址
* @param subject 主题
* @param text 正文
* @param file 内联文件
* @throws MessagingException
*/
void sendInlineMail(String deliveryAddress, String subject, String text, File file) throws MessagingException;
/**
* 发送模板邮件
*
* @param deliveryAddress 收件人地址
* @param subject 主题
* @param templateName 模板文件
* @param contentVariableMap 模板内占位数据
* @throws MessagingException
*/
void sendTemplateMail(String deliveryAddress, String subject, String templateName,
Map<String, Object> contentVariableMap) throws MessagingException;
}
邮件服务接口实现
/**
* @author zhu
* @description: 邮件服务接口实现类
* @date 2019-11-04 16:32
*/
@Service
public class MailServiceImpl implements MailService {
@Autowired
private JavaMailSender mailSender;
@Autowired
private TemplateEngine templateEngine;
// 读取配置文件中的参数
@Value("${spring.mail.username}")
private String sender;
@Override
public void sendSimpleMail(String deliveryAddress, String subject, String text) {
SimpleMailMessage message = new SimpleMailMessage();
// 发送者邮箱帐号
message.setFrom(sender);
// 接收者邮箱地址
message.setTo(deliveryAddress);
message.setSubject(subject);
message.setText(text);
mailSender.send(message);
}
@Override
public void sendAttachmentsMail(String deliveryAddress, String subject, String text, File[] files) throws MessagingException {
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
// 发送者邮箱帐号
helper.setFrom(sender);
// 接收者邮箱帐号
helper.setTo(deliveryAddress);
helper.setSubject(subject);
helper.setText(text);
for (File file : files) {
// 附件名称,注意附件后缀。
helper.addAttachment(file.getName(), new FileSystemResource(file));
}
mailSender.send(mimeMessage);
}
@Override
public void sendInlineMail(String deliveryAddress, String subject, String text, File file) throws MessagingException {
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
// 发送者邮箱帐号
helper.setFrom(sender);
// 接收者邮箱帐号
helper.setTo(deliveryAddress);
helper.setSubject(subject);
// cid 与文件名需要相同,不需要后缀名
helper.setText(text, true);
FileSystemResource fileSystemResource = new FileSystemResource(file);
// 与文件名需要相同,不需要后缀名
helper.addInline(file.getName().substring(0, file.getName().lastIndexOf(".")), fileSystemResource);
mailSender.send(mimeMessage);
}
@Override
public void sendTemplateMail(String deliveryAddress, String subject, String templateName, Map<String, Object> contentVariableMap) throws MessagingException {
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
helper.setFrom(sender);
helper.setTo(deliveryAddress);
helper.setSubject(subject);
//设置 map 形式数据
Context context = new Context();
context.setVariables(contentVariableMap);
//获取模板 html 代码
String text = templateEngine.process(templateName, context);
helper.setText(text, true);
mailSender.send(mimeMessage);
}
}
二、测试
2.1 测试简单邮件
测试方法:
@SpringBootTest
public class MailServiceImplTest {
@Autowired
private MailService mailService;
private String deliveryAddress;
@BeforeEach
public void setUp() {
deliveryAddress = "marklogzhu@163.com";
}
@Test
public void testSendSimpleMail() {
String subject = "主题:简单邮件";
String text = "测试邮件内容";
mailService.sendSimpleMail(deliveryAddress,subject,text);
}
}
执行方法,查看邮箱
2.2 测试带附件邮件
测试方法:
@SpringBootTest
public class MailServiceImplTest {
@Autowired
private MailService mailService;
private String deliveryAddress;
@BeforeEach
public void setUp() {
deliveryAddress = "marklogzhu@163.com";
}
@Test
public void testSendAttachmentsMail() throws MessagingException {
String subject = "主题:有附件";
String text = "有附件的邮件";
File[] files = new File[]{new File("C:\Users\zhu\Pictures\Camera Roll\desktop.png")};
mailService.sendAttachmentsMail(deliveryAddress, subject, text, files);
}
}
执行方法,查看邮箱
2.3 测试带内联邮件
测试方法:
@SpringBootTest
public class MailServiceImplTest {
@Autowired
private MailService mailService;
private String deliveryAddress;
@BeforeEach
public void setUp() {
deliveryAddress = "marklogzhu@163.com";
}
@Test
public void testSendInlineMail() throws MessagingException {
String subject = "主题:嵌入静态资源";
String text = "<html><body><img src=\"cid:desktop\" ></body></html>";
File file = new File("C:\Users\zhu\Pictures\Camera Roll\desktop.png");
mailService.sendInlineMail(deliveryAddress, subject, text, file);
}
}
执行方法,查看邮箱
2.4 测试模板邮件
引入模板依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
在 resources/templates
目录下新建模板文件 mailTemplate.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<body>
<h3>你好, <h2 th:text="${username}"></h2>, 这是一封模板邮件!</h3>
</body>
</html>
测试方法:
@SpringBootTest
public class MailServiceImplTest {
@Autowired
private MailService mailService;
private String deliveryAddress;
@BeforeEach
public void setUp() {
deliveryAddress = "marklogzhu@163.com";
}
}
执行方法,查看邮箱