Spring Boot中使用JavaMailSender发送邮件
一 点睛
使用过Spring的众多开发者都知道Spring提供了非常好用的JavaMailSender接口实现邮件发送。
在Spring Boot的Starter模块中也为此提供了自动化配置。
下面通过实例看看如何在Spring Boot中使用JavaMailSender发送邮件。
二 实战
1 引入相关依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-velocity</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2 application.properties配置
spring.mail.host=smtp.qq.com
spring.mail.username=798103175@qq.com
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
3 准备一个邮件模板
<html>
<body>
<h3>你好, ${username}, 这是一封模板邮件!</h3>
</body>
</html>
4 启动类
package com.didispace;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
5 测试类
package com.didispace;
import org.apache.commons.collections.map.HashedMap;
import org.apache.velocity.app.VelocityEngine;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.ui.velocity.VelocityEngineUtils;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Map;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
public class ApplicationTests {
@Autowired
private JavaMailSender mailSender;
@Autowired
private VelocityEngine velocityEngine;
@Test
public void sendSimpleMail() throws Exception {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom("798103175@qq.com");
message.setTo("798103175@qq.com");
message.setSubject("主题:简单邮件");
message.setText("测试邮件内容");
/*
由于Spring Boot的starter模块提供了自动化配置,
所以在引入了spring-boot-starter-mail依赖之后,
会根据配置文件中的内容去创建JavaMailSender实例,
因此我们可以直接在需要使用的地方直接@Autowired来引入邮件发送对象。
* */
mailSender.send(message);
}
@Test
public void sendAttachmentsMail() throws Exception {
/*
有些邮件会带上附件
这个时候我们就需要使用MimeMessage来设置复杂一些的邮件内容
* */
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
helper.setFrom("798103175@qq.com");
helper.setTo("798103175@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);
}
@Test
public void sendInlineMail() throws Exception {
/*
有些邮件会带上静态资源
这个时候我们就需要使用MimeMessage来设置复杂一些的邮件内容
* */
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
helper.setFrom("798103175@qq.com");
helper.setTo("798103175@qq.com");
helper.setSubject("主题:嵌入静态资源");
helper.setText("<html><body><img src=\"cid:photo\" ></body></html>", true);
FileSystemResource file = new FileSystemResource(new File("photo.jpg"));
//这里需要注意的是addInline函数中资源名称photo需要与正文中cid:photo对应起来
helper.addInline("photo", file);
mailSender.send(mimeMessage);
}
@Test
public void sendTemplateMail() throws Exception {
/*
通常我们使用邮件发送服务的时候,都会有一些固定的场景,比如重置密码、注册确认等,
给每个用户发送的内容可能只有小部分是变化的。所以,很多时候我们会使用模板引擎来
为各类邮件设置成模板,这样我们只需要在发送时去替换变化部分的参数即可。
这个时候我们就需要使用MimeMessage来设置复杂一些的邮件内容
* */
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
helper.setFrom("798103175@qq.com");
helper.setTo("798103175@qq.com");
helper.setSubject("主题:模板邮件");
Map<String, Object> model = new HashedMap();
model.put("username", "cakin");
String text = VelocityEngineUtils.mergeTemplateIntoString(
velocityEngine, "template.vm", "UTF-8", model);
helper.setText(text, true);
mailSender.send(mimeMessage);
}
}
三 测试
1 简单邮件测试
2 有附件邮件测试
3 嵌入静态资源邮件
4 模块邮件
四参考
blog.csdn.net/qq_24452475/article/...
本作品采用《CC 协议》,转载必须注明作者和本文链接
建议在java板块发。 :grin: