Spring Boot 2发送邮件

Spring Boot发送邮件:

本文基于:Spring Boot 2.1.3,理论支持Spring Boot 2.x所有版本。

使⽤JavaMailSender发送邮件
相信使⽤过Spring的众多开发者都知道Spring提供了⾮常好⽤的 JavaMailSender 接⼝实现邮件发
送。在Spring Boot的Starter模块中也为此提供了⾃动化配置。下⾯通过实例看看如何在Spring Boot中
使⽤ JavaMailSender 发送邮件。
快速⼊⻔
在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=密码     QQ邮箱->设置->账户->POP3/SMTP服务:开启服务后会获得QQ的授权码.
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("**@qq.com");
    message.setTo("**@qq.com");
    message.setSubject("主题:简单邮件");
    message.setText("测试邮件内容");
    mailSender.send(message);
  }
}

到这⾥,⼀个简单的邮件发送就完成了,运⾏⼀下该单元测试,看看效果如何?
由于Spring Boot的starter模块提供了⾃动化配置,所以在引⼊了 spring-boot-startermail 依赖之后,会根据配置⽂件中的内容去创建 JavaMailSender 实例,因此我们可以直接在
需要使⽤的地⽅直接 @Autowired 来引⼊邮件发送对象。

 

进阶使⽤


在上例中,我们通过使⽤ SimpleMailMessage 实现了简单的邮件发送,但是实际使⽤过程中,我们还
可能会带上附件、或是使⽤邮件模块等。这个时候我们就需要使⽤ MimeMessage 来设置复杂⼀些的邮
件内容,下⾯我们就来依次实现⼀下。
发送附件
在上⾯单元测试中加⼊如下测试⽤例(通过MimeMessageHelper来发送⼀封带有附件的邮件):

@Test
public void sendAttachmentsMail() throws Exception {
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
helper.setFrom("**@qq.com");
helper.setTo("**@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("**@qq.com");
helper.setTo("*@qq.com");
helper.setSubject("主题:嵌⼊静态资源1");
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 需要与正⽂中src  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("**@qq.com");
helper.setTo("**@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} 变量。

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用 Spring Boot邮件发送功能来发送电子邮件。首先,你需要在项目的依赖中添加 Spring Boot 的邮件依赖。在 `pom.xml` 文件中添加以下代码: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> ``` 接下来,你需要在配置文件中配置邮件相关的属性。在 `application.properties`(或 `application.yml`)文件中添加以下属性: ```properties # 邮件服务器主机名 spring.mail.host=your-mail-host # 邮件服务器端口 spring.mail.port=your-mail-port # 邮件发送者用户名 spring.mail.username=your-username # 邮件发送者密码 spring.mail.password=your-password # 邮件发送者地址 spring.mail.from=your-email-address ``` 现在,你可以在你的代码中使用 `JavaMailSender` 接口来发送邮件。你可以注入 `JavaMailSender` 接口的实例,并使用 `send()` 方法发送邮件。以下是一个简单的示例: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.stereotype.Service; @Service public class EmailService { @Autowired private JavaMailSender javaMailSender; public void sendEmail(String to, String subject, String text) { SimpleMailMessage message = new SimpleMailMessage(); message.setTo(to); message.setSubject(subject); message.setText(text); javaMailSender.send(message); } } ``` 你可以在需要发送邮件的地方调用 `sendEmail()` 方法,并传入收件人地址、邮件主题和邮件内容。 这是使用 Spring Boot 发送邮件的基本步骤。你可以根据自己的需求进行进一步的定制和配置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值