1 添加pom.xml 文件
<?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>com.neo</groupId>
<artifactId>spring-boot-mail</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>spring-boot-mail</name>
<description>Demo project for Spring Boot and mail</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>RELEASE</version>
</dependency>
<!-- 模板引擎 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
</project>
2 涉及相关类
JavaMailSender : mail 发送类,使用此类进行邮件发送。
mailSender.send(message);
发送Mail时要构建Message , Message 构建可以使用
// 复杂内容发送
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
支持HTML 形式发送,内嵌图片,链接方式,附件形式,模板邮件方式。
// 添加附件方式
FileSystemResource file = new FileSystemResource(new File(filePath));
String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
helper.addAttachment(fileName, file);
// 添加内嵌资源形式
FileSystemResource res = new FileSystemResource(new File(rscPath));
helper.addInline(rscId, res);
mailSender.send(message);
// 简单文本发送
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(from);
message.setTo(to);
message.setSubject(subject);
message.setText(content);
mailSender.send(message);
3 邮件发送属性配置
spring.application.name=spirng-boot-mail
spring.mail.host=smtp.163.com
spring.mail.username=xxx@163.com
spring.mail.password=
spring.mail.default-encoding=UTF-8
mail.fromMail.addr=calvins
4 代码测试
//模板方式调用
@Autowired
private MailService mailService;
@Autowired
private TemplateEngine templateEngine;
@Test
public void testTemplateMail() {
//创建邮件正文
Context context = new Context();
context.setVariable("id", "006");
String emailContent = templateEngine.process("emailTemplate", context);
mailService.sendHtmlMail(toMail,"主题:这是模板邮件",emailContent);
}
模板使用 spring-boot-starter-thymeleaf , 会自动从resource/templates 下加载html 文件。
总结:SpringBoot 集成了mail 邮件服务,能快速的完成邮件发送的开发工作。主要就关注几个类,一个是发送类的实现。使用接口 JavaMailSender 的send 方法,一个message 构造,简单文本使用 SimpleMailMessage message = new SimpleMailMessage(); 复杂内容使用:MimeMessage message = mailSender.createMimeMessage(); 附件、内嵌资源借助MimeMessageHelper helper = new MimeMessageHelper(message, true); 来构建。