原文:http://www.cnblogs.com/yucongblog/p/7385434.htmlhtml
相信使用过Spring的众多开发者都知道Spring提供了很是好用的JavaMailSender
接口实现邮件发送。在Spring Boot的Starter模块中也为此提供了自动化配置。下面经过实例看看如何在Spring Boot中使用JavaMailSender
发送邮件。spring
在Spring Boot的工程中的pom.xml
中引入spring-boot-starter-mail
依赖:app
1
2
3
4
|
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
|
如其余自动化配置模块同样,在完成了依赖引入以后,只须要在application.properties
中配置相应的属性内容。函数
下面咱们以QQ邮箱为例,在application.properties
中加入以下配置(注意替换本身的用户名和密码):spring-boot
1
2
3
4
5
6
|
spring.mail.host=smtp.qq.com
spring.mail.username=用户名
spring.mail.password=密码
spring.mail.properties.mail.smtp.auth=
true
spring.mail.properties.mail.smtp.starttls.enable=
true
spring.mail.properties.mail.smtp.starttls.required=
true
|
经过单元测试来实现一封简单邮件的发送:单元测试
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
public class ApplicationTests {
private JavaMailSender mailSender;
public void sendSimpleMail() throws Exception {
SimpleMailMessage message =
new SimpleMailMessage();
message.setFrom(
"dyc87112@qq.com");
message.setTo(
"dyc87112@qq.com");
message.setSubject(
"主题:简单邮件");
message.setText(
"测试邮件内容");
mailSender.send(message);
}
}
|
到这里,一个简单的邮件发送就完成了,运行一下该单元测试,看看效果如何?测试
因为Spring Boot的starter模块提供了自动化配置,因此在引入了
spring-boot-starter-mail
依赖以后,会根据配置文件中的内容去建立JavaMailSender
实例,所以咱们能够直接在须要使用的地方直接@Autowired
来引入邮件发送对象。ui
在上例中,咱们经过使用SimpleMailMessage
实现了简单的邮件发送,可是实际使用过程当中,咱们还可能会带上附件、或是使用邮件模块等。这个时候咱们就须要使用MimeMessage
来设置复杂一些的邮件内容,下面咱们就来依次实现一下。spa
在上面单元测试中加入以下测试用例(经过MimeMessageHelper来发送一封带有附件的邮件):code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
public void sendAttachmentsMail() throws Exception {
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper =
new MimeMessageHelper(mimeMessage, true);
helper.setFrom(
"dyc87112@qq.com");
helper.setTo(
"dyc87112@qq.com");
helper.setSubject(
"主题:有附件");
helper.setText(
"有附件的邮件");
FileSystemResource file =
new FileSystemResource(new File("weixin.jpg"));
helper.addAttachment(
"附件-1.jpg", file);
helper.addAttachment(
"附件-2.jpg", file);
mailSender.send(mimeMessage);
}
|
除了发送附件以外,咱们在邮件内容中可能但愿经过嵌入图片等静态资源,让邮件得到更好的阅读体验,而不是从附件中查看具体图片,下面的测试用例演示了如何经过MimeMessageHelper
实如今邮件正文中嵌入静态资源。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
public void sendInlineMail() throws Exception {
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper =
new MimeMessageHelper(mimeMessage, true);
helper.setFrom(
"dyc87112@qq.com");
helper.setTo(
"dyc87112@qq.com");
helper.setSubject(
"主题:嵌入静态资源");
helper.setText(
"<html><body><img src=\"cid:weixin\" ></body></html>", true);
FileSystemResource file =
new FileSystemResource(new File("weixin.jpg"));
helper.addInline(
"weixin", file);
mailSender.send(mimeMessage);
}
|
这里须要注意的是addInline
函数中资源名称weixin
须要与正文中cid:weixin
对应起来
一般咱们使用邮件发送服务的时候,都会有一些固定的场景,好比重置密码、注册确认等,给每一个用户发送的内容可能只有小部分是变化的。因此,不少时候咱们会使用模板引擎来为各种邮件设置成模板,这样咱们只须要在发送时去替换变化部分的参数便可。
在Spring Boot中使用模板引擎来实现模板化的邮件发送也是很是容易的,下面咱们以velocity为例实现一下。
引入velocity模块的依赖:
1
2
3
4
|
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-velocity</artifactId>
</dependency>
|
在resources/templates/
下,建立一个模板页面template.vm
:
1
2
3
4
5
|
<html>
<body>
<h3>你好, ${username}, 这是一封模板邮件!</h3>
</body>
</html>
|
咱们以前在Spring Boot中开发Web应用时,提到过在Spring Boot的自动化配置下,模板默认位于resources/templates/
目录下
最后,咱们在单元测试中加入发送模板邮件的测试用例,具体以下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
public void sendTemplateMail() throws Exception {
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper =
new MimeMessageHelper(mimeMessage, true);
helper.setFrom(
"dyc87112@qq.com");
helper.setTo(
"dyc87112@qq.com");
helper.setSubject(
"主题:模板邮件");
Map<String, Object> model =
new HashedMap();
model.put(
"username", "didi");
String text = VelocityEngineUtils.mergeTemplateIntoString(
velocityEngine,
"template.vm", "UTF-8", model);
helper.setText(text,
true);
mailSender.send(mimeMessage);
}
|
尝试运行一下,就能够收到内容为你好, didi, 这是一封模板邮件!
的邮件。这里,咱们经过传入username的参数,在邮件内容中替换了模板中的${username}
变量。