Spring Boot中使用JavaMailSender发送邮件

Stella981
• 阅读 587

相信使用过Spring的众多开发者都知道Spring提供了非常好用的 JavaMailSender接口实现邮件发送。在Spring Boot的Starter模块中也为此提供了自动化配置。下面通过实例看看如何在Spring Boot中使用 JavaMailSender发送邮件。

快速入门

在Spring Boot的工程中的 pom.xml中引入 spring-boot-starter-mail依赖:

  1. <dependency>

  2. <groupId>org.springframework.boot</groupId>

  3. <artifactId>spring-boot-starter-mail</artifactId>

  4. </dependency>

如其他自动化配置模块一样,在完成了依赖引入之后,只需要在 application.properties中配置相应的属性内容。

下面我们以QQ邮箱为例,在 application.properties中加入如下配置(注意替换自己的用户名和密码):

  1. spring.mail.host=smtp.qq.com

  2. spring.mail.username=用户名

  3. spring.mail.password=密码

  4. spring.mail.properties.mail.smtp.auth=true

  5. spring.mail.properties.mail.smtp.starttls.enable=true

  6. spring.mail.properties.mail.smtp.starttls.required=true

通过单元测试来实现一封简单邮件的发送:

  1. @RunWith(SpringJUnit4ClassRunner.class)

  2. @SpringApplicationConfiguration(classes = Application.class)

  3. public class ApplicationTests {

  4. @Autowired

  5. private JavaMailSender mailSender;

  6. @Test

  7. public void sendSimpleMail() throws Exception {

  8. SimpleMailMessage message = new SimpleMailMessage();

  9. message.setFrom("dyc87112@qq.com");

  10. message.setTo("dyc87112@qq.com");

  11. message.setSubject("主题:简单邮件");

  12. message.setText("测试邮件内容");

  13. mailSender.send(message);

  14. }

  15. }

到这里,一个简单的邮件发送就完成了,运行一下该单元测试,看看效果如何?

由于Spring Boot的starter模块提供了自动化配置,所以在引入了 spring-boot-starter-mail依赖之后,会根据配置文件中的内容去创建 JavaMailSender实例,因此我们可以直接在需要使用的地方直接 @Autowired来引入邮件发送对象。

进阶使用

在上例中,我们通过使用 SimpleMailMessage实现了简单的邮件发送,但是实际使用过程中,我们还可能会带上附件、或是使用邮件模块等。这个时候我们就需要使用 MimeMessage来设置复杂一些的邮件内容,下面我们就来依次实现一下。

发送附件

在上面单元测试中加入如下测试用例(通过MimeMessageHelper来发送一封带有附件的邮件):

  1. @Test

  2. public void sendAttachmentsMail() throws Exception {

  3. MimeMessage mimeMessage = mailSender.createMimeMessage();

  4. MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);

  5. helper.setFrom("dyc87112@qq.com");

  6. helper.setTo("dyc87112@qq.com");

  7. helper.setSubject("主题:有附件");

  8. helper.setText("有附件的邮件");

  9. FileSystemResource file = new FileSystemResource(new File("weixin.jpg"));

  10. helper.addAttachment("附件-1.jpg", file);

  11. helper.addAttachment("附件-2.jpg", file);

  12. mailSender.send(mimeMessage);

  13. }

嵌入静态资源

除了发送附件之外,我们在邮件内容中可能希望通过嵌入图片等静态资源,让邮件获得更好的阅读体验,而不是从附件中查看具体图片,下面的测试用例演示了如何通过 MimeMessageHelper实现在邮件正文中嵌入静态资源。

  1. @Test

  2. public void sendInlineMail() throws Exception {

  3. MimeMessage mimeMessage = mailSender.createMimeMessage();

  4. MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);

  5. helper.setFrom("dyc87112@qq.com");

  6. helper.setTo("dyc87112@qq.com");

  7. helper.setSubject("主题:嵌入静态资源");

  8. helper.setText("<html><body><img src=\"cid:weixin\" ></body></html>", true);

  9. FileSystemResource file = new FileSystemResource(new File("weixin.jpg"));

  10. helper.addInline("weixin", file);

  11. mailSender.send(mimeMessage);

  12. }

这里需要注意的是 addInline函数中资源名称 weixin需要与正文中 cid:weixin对应起来

模板邮件

通常我们使用邮件发送服务的时候,都会有一些固定的场景,比如重置密码、注册确认等,给每个用户发送的内容可能只有小部分是变化的。所以,很多时候我们会使用模板引擎来为各类邮件设置成模板,这样我们只需要在发送时去替换变化部分的参数即可。

在Spring Boot中使用模板引擎来实现模板化的邮件发送也是非常容易的,下面我们以velocity为例实现一下。

引入velocity模块的依赖:

  1. <dependency>

  2. <groupId>org.springframework.boot</groupId>

  3. <artifactId>spring-boot-starter-velocity</artifactId>

  4. </dependency>

resources/templates/下,创建一个模板页面 template.vm

  1. <html>

  2. <body>

  3. <h3>你好, ${username}, 这是一封模板邮件!</h3>

  4. </body>

  5. </html>

我们之前在Spring Boot中开发Web应用时,提到过在Spring Boot的自动化配置下,模板默认位于 resources/templates/目录下

最后,我们在单元测试中加入发送模板邮件的测试用例,具体如下:

  1. @Test

  2. public void sendTemplateMail() throws Exception {

  3. MimeMessage mimeMessage = mailSender.createMimeMessage();

  4. MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);

  5. helper.setFrom("dyc87112@qq.com");

  6. helper.setTo("dyc87112@qq.com");

  7. helper.setSubject("主题:模板邮件");

  8. Map<String, Object> model = new HashedMap();

  9. model.put("username", "didi");

  10. String text = VelocityEngineUtils.mergeTemplateIntoString(

  11. velocityEngine, "template.vm", "UTF-8", model);

  12. helper.setText(text, true);

  13. mailSender.send(mimeMessage);

  14. }

尝试运行一下,就可以收到内容为 你好,didi,这是一封模板邮件!的邮件。这里,我们通过传入username的参数,在邮件内容中替换了模板中的 ${username}变量。

长按指纹

一键关注

Spring Boot中使用JavaMailSender发送邮件

Spring Boot中使用JavaMailSender发送邮件

好书推荐            

Spring Boot中使用JavaMailSender发送邮件

点击“阅读原文”

本文分享自微信公众号 - 程序猿DD(didispace)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。

点赞
收藏
评论区
推荐文章
Easter79 Easter79
2年前
sprintboot
一、邮件发送使用springboot自带的邮件系统就能实现邮件的发送,首先导入依赖:1、新建springboot项目,添加依赖<dependency<groupIdorg.springframework.boot</groupId<artifactIdspringbootstartermail
Stella981 Stella981
2年前
Spring Boot 2发送邮件手把手图文教程
点击上方 IT牧场 ,选择 置顶或者星标技术干货每日送达!本文基于:Spring Boot 2.1.3,理论支持Spring Boot 2.x所有版本。最近有童鞋问到笔者如何用Spring Boot发送邮件,故而整理下Spring Boot发送邮件的各种姿势。说到邮件放松,相信大家对SpringFramework提供的接
Stella981 Stella981
2年前
SpringBoot使用JavaMailSender发送邮件(2)
为了更容易更规范维护邮件内容,我们推荐使用模板引擎技术。常用的模板引擎有这几种Thymeleaf、FreeMarker、Velocity等。我们在这里就使用前面两种来实现发送邮件。一、使用Thymeleaf模板技术发送邮件Thymeleaf是SpringBoot推荐的官方模板引擎技术,使用非常的简单方便。(1)添加依赖
Stella981 Stella981
2年前
Jenkins 配置邮件通知步骤
Jenkins配置邮件通知前言可以在Jenkins中配置邮件通知,比如在构建失败时发送邮件通知项目组来及时修复问题。Jenkins邮件通知功能的插件主要包括:MailerPlugin(默认的发送邮件插件)EmailExtensionPlugin(功能更强大的发送邮件插件)建议同时安装这两个插件。下面来介绍如何在
Easter79 Easter79
2年前
SpringBoot使用JavaMailSender发送邮件(1)
邮件发送是一个非常常见的功能,最初Sun公司提供了JavaMail用来实现邮件发送,但是配置烦琐。后来Spring中提供了JavaMailsender用来简化邮件配置,而SpringBoot则提供了MailSenderAutoConfiguration对邮件的发送做了进一步简化。在开始之前我们需要申请开通POP3/SMTP服
Easter79 Easter79
2年前
SpringBoot使用JavaMailSender发送邮件(2)
为了更容易更规范维护邮件内容,我们推荐使用模板引擎技术。常用的模板引擎有这几种Thymeleaf、FreeMarker、Velocity等。我们在这里就使用前面两种来实现发送邮件。一、使用Thymeleaf模板技术发送邮件Thymeleaf是SpringBoot推荐的官方模板引擎技术,使用非常的简单方便。(1)添加依赖
Stella981 Stella981
2年前
Spring Boot 与 Kotlin 定时任务(Scheduling Tasks)
在编写SpringBoot应用中会遇到这样的场景,比如:需要定时地发送一些短信、邮件之类的操作,也可能会定时地检查和监控一些标志、参数等。创建定时任务在SpringBoot中编写定时任务是非常简单的事,下面通过实例介绍如何在SpringBoot中创建定时任务,实现每过5秒输出一下当前时间。在SpringBoot的主类中加入
Stella981 Stella981
2年前
Spring Boot demo系列(七):邮件服务
2021.2.24更新1概述SpringBoot整合邮件服务,包括发送普通的文本邮件以及带附件的邮件。2邮箱选择这里选择的是QQ邮箱作为发送的邮箱,当然也可以选择其他的邮箱,只是具体的配置不一样。使用QQ邮箱的话,需要在个人设置中开启SMTP服务:!在这里插入
Stella981 Stella981
2年前
SpringBoot使用JavaMailSender发送邮件(1)
邮件发送是一个非常常见的功能,最初Sun公司提供了JavaMail用来实现邮件发送,但是配置烦琐。后来Spring中提供了JavaMailsender用来简化邮件配置,而SpringBoot则提供了MailSenderAutoConfiguration对邮件的发送做了进一步简化。在开始之前我们需要申请开通POP3/SMTP服
Stella981 Stella981
2年前
SpringBoot 2.x 集成QQ邮箱、网易系邮箱、Gmail邮箱发送邮件
在Spring中提供了非常好用的JavaMailSender接口实现邮件发送,在SpringBoot的Starter模块中也为此提供了自动化配置。项目源码已托管在GiteeSpringBoot\_Guide(https://gitee.com/javen205/SpringBoot_Guide.git"SpringBoot_Guide")