这篇文章主要介绍了spring boot中利用javamailsender发送邮件的方法示例, 相信使用过spring的众多开发者都知道spring提供了非常好用的javamailsender接口实现邮件发送。在spring boot的starter模块中也为此提供了自动化配置。需要的朋友可以参考借鉴。
快速入门
在spring boot的工程中的pom.xml中引入spring-boot-starter-mail依赖:
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-mail</artifactid>
</dependency>
如其他自动化配置模块一样,在完成了依赖引入之后,只需要在application.properties中配置相应的属性内容。
下面我们以qq邮箱为例,在application.properties中加入如下配置(注意替换自己的用户名和密码):
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
通过单元测试来实现一封简单邮件的发送:
@runwith(springjunit4classrunner.class)
@springapplicationconfiguration(classes = application.class)
public class applicationtests {
@autowired
private javamailsender mailsender;
@test
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来引入邮件发送对象。”
进阶使用
在上例中,我们通过使用simplemailmessage实现了简单的邮件发送,但是实际使用过程中,我们还可能会带上附件、或是使用邮件模块等。这个时候我们就需要使用mimemessage来设置复杂一些的邮件内容,下面我们就来依次实现一下。
发送附件
在上面单元测试中加入如下测试用例(通过mimemessagehelper来发送一封带有附件的邮件):
@test
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实现在邮件正文中嵌入静态资源。
@test
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模块的依赖:
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-velocity</artifactid>
</dependency>
在resources/templates/下,创建一个模板页面template.vm:
<html>
<body>
<h3>你好, ${username}, 这是一封模板邮件!</h3>
</body>
</html>
我们之前在spring boot中开发web应用时,提到过在spring boot的自动化配置下,模板默认位于resources/templates/目录下
最后,我们在单元测试中加入发送模板邮件的测试用例,具体如下:
@test
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}变量。
以上就是spring boot中利用javamailsender发送邮件的方法示例(附源码)的详细内容。