spring-boot-mail
Project structure
1. Maven project depends on coordinates
Note: SpringBoot version needs to be 2.x
若spring boot版本为1.x,
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>thymeleaf-action</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<artifactId>spring-boot-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.0.7.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
</project>
2. Spring Boot configuration file
#开发环境下,关闭Thymeleaf缓存;生产环境需开启增强并发能力
spring.thymeleaf.cache=false
#邮件服务器地址,已QQ邮箱为例
spring.mail.host=smtp.qq.com
#邮件服务器端口
spring.mail.port=587
#发件人邮箱地址
spring.mail.username=发件人邮箱地址
#发件人邮箱密码
spring.mail.password=发件人邮箱密码
# 构建授权信息,用于进行SMTP进行身份验证
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
3. Mail tools
package org.example.utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Map;
@Component
public class MailTool {
@Autowired
private JavaMailSender javaMailSender;
@Value("${spring.mail.username}")
private String senderMailAddress;
@Autowired
private TemplateEngine templateEngine;
public void sendSimpleMail(Map<String, Object> valueMap) {
MimeMessage mimeMessage = null;
try {
mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
//设置发件人邮箱
helper.setFrom(senderMailAddress);
//设置收件人邮箱
helper.setTo((String[]) valueMap.get("to"));
//设置邮件标题
helper.setSubject(valueMap.get("title").toString());
//添加正文(使用thymeleaf模板)
Context context = new Context();
context.setVariables(valueMap);
String content = templateEngine.process("mail", context);
helper.setText(content, true);
//添加附件
if (valueMap.get("filePathList") != null) {
String[] filePathList = (String[]) valueMap.get("filePathList");
for (String filePath : filePathList) {
FileSystemResource fileSystemResource = new FileSystemResource(new File(filePath));
String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
helper.addAttachment(fileName, fileSystemResource);
}
}
//发送邮件
javaMailSender.send(mimeMessage);
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
4. Test code
package org.example.utils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.HashMap;
import java.util.Map;
import static org.junit.Assert.*;
@SpringBootTest
@RunWith(SpringRunner.class)
public class MailToolTest {
@Autowired
private MailTool mailTool;
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void sendSimpleMail() {
String[] filePathList = new String[]{"文件地址1","文件地址2","文件地址3",};
Map<String, Object> valueMap = new HashMap<String, Object>();
valueMap.put("to", new String[]{"收件人1", "收件人2", "收件人3"});
valueMap.put("title", "测试邮件标题");
valueMap.put("content","测试邮件内容");
valueMap.put("filePathList", filePathList);
mailTool.sendSimpleMail(valueMap);
}
}
Attachment: successful registration email html code template
<div style="background-color:#ECECEC; padding: 35px;">
<table cellpadding="0" align="center"
style="width: 600px; 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: #42a3d3; background-color: #49bcff; 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); ">注册成功! (阿里云)</font>
</th>
</tr>
<tr>
<td>
<div style="padding:25px 35px 40px; background-color:#fff;">
<h2 style="margin: 5px 0px; ">
<font color="#333333" style="line-height: 20px; ">
<font style="line-height: 22px; " size="4">
亲爱的 123456</font>
</font>
</h2>
<p>首先感谢您加入本**站!下面是您的账号信息<br>
您的账号:<b>123456</b><br>
您的密码:<b>123456</b><br>
您的邮箱:<b>123@**.com</b><br>
您注册时的日期:<b>2019年06月17天23时33分20秒</b><br>
您注册时的IP:<b>221.230.56.12</b><br>
您注册的地址:<b>江苏省镇江市</b><br>
当您在使用本网站时,遵守当地法律法规。<br>
如果您有什么疑问可以联系管理员,Email: **@**.com</p>
<p align="right">BER分接口网</p>
<p align="right">2019年06月17号 23时33分20秒</p>
<div style="width:700px;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>此为系统邮件,请勿回复<br>
请保管好您的邮箱,避免账号被他人盗用
</p>
<p>©***</p>
</div>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>