前言
Spring Boot系列: 点击查看Spring Boot系列文章
Spring Boot发送邮件
在开发中,有时候我们需要在项目中发送邮件。其实,在Spring Boot中发送邮件是很简单的,下面我们就来看看
1、添加邮件依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
2、在配置文件进行邮箱配置,我们发送邮件,需要先获取邮箱的授权码
163邮箱配置
spring.mail.host=smtp.163.com #邮箱服务器地址
spring.mail.username=xxx.163.com #用户名
spring.mail.password=ooo #开启POP3之后设置的客户端授权码
spring.mail.default-encoding=UTF-8 #编码
qq邮箱配置
spring.mail.host=smtp.qq.com
spring.mail.username=xxx@qq.com
spring.mail.password=ooo #开启POP3之后设置的客户端授权码
spring.mail.default-encoding=UTF-8
126邮箱配置
spring.mail.host=smtp.126.com
spring.mail.username=xxx.126.com
spring.mail.password=ooo #开启POP3之后设置的客户端授权码
spring.mail.default-encoding=UTF-8
一些通用的配置
# 超时时间
spring.mail.properties.mail.smtp.connectiontimeout=5000
spring.mail.properties.mail.smtp.timeout=3000
spring.mail.properties.mail.smtp.writetimeout=5000
# 设置是否需要认证,如果为true,那么用户名和密码就必须的,
#如果设置false,可以不设置用户名和密码,当然也得看你的对接的平台是否支持无密码进行访问的。
spring.mail.properties.mail.smtp.auth=true
# STARTTLS[1] 是对纯文本通信协议的扩展。它提供一种方式将纯文本连接升级为加密连接(TLS或SSL),而不是另外使用一个端口作加密通信。
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
3、配置了配置信息后,Spring Boot已经帮我们内置了JavaMailSender,我们可以直接引入JavaMailSender发送邮件
下面是一个发送邮件的工具类
@Component
public class MailSendUtil {
@Autowired
private JavaMailSender mailSender;
@Value("${spring.mail.username}")
private String sender;
/**
* 发送文本邮件
* @param to 邮件接受者
* @param subject 主题
* @param content 内容
* @throws MailException
*/
public void sendSimpleMail(String to, String subject, String content) throws MailException {
SimpleMailMessage message = new SimpleMailMessage();
// 邮件发送者
message.setFrom(sender);
message.setTo(to);
message.setSubject(subject);
message.setText(content);
mailSender.send(message);
}
/**
* 发送带附件的邮件
* @param to
* @param subject
* @param content
* @param filePath
* @throws MessagingException
*/
public void sendAttachmentsMail(String to, String subject, String content, String filePath) throws MessagingException {
MimeMessage message = mailSender.createMimeMessage();
//MimeMessageHelper支持发送复杂的邮件模板
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(sender);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
FileSystemResource file = new FileSystemResource(new File(filePath));
String fileName = file.getFilename();
//addAttachment方法即为添加附件的方法
helper.addAttachment(fileName, file);
//addInline为添加图片,第一个是图片ID,第二个是图片文件Resource
// helper.addInline()
mailSender.send(message);
}
/**
* 发送HTML邮件
* @param to 接收者邮箱
* @param subject 主题
* @param content 内容
* @throws MessagingException
*/
public void sendHtmlMail(String to, String subject, String content) throws MessagingException {
MimeMessage message = mailSender.createMimeMessage();
//true 表⽰示需要创建⼀一个 multipart message
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(sender);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
mailSender.send(message);
}
}
测试发送普通邮件
@SpringBootTest
class SpringCloudDemoApplicationTests {
@Autowired
private MailSendUtil sendUtil;
@Test
public void test6(){
sendUtil.sendSimpleMail("msq_myemail@163.com","测试邮件","测试");
}
}
结果如下,成功收到测试邮件