引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
yaml配置
spring:
mail:
host: 邮箱地址
username: 账号
password: 密码
default-encoding: UTF-8 #邮件编码
properties:
mail.smtp.auth: true # 设置是否需要认证,如果为true,那么用户名和密码就必须的。# 如果设置false,可以不设置用户名和密码,当然也得看你的对接的平台是否支持无密码进行访问的。
mail.smtp.timeout: 2000 #超时时间
# STARTTLS[1] 是对纯文本通信协议的扩展。它提供一种方式将纯文本连接升级为加密连接(TLS或SSL),而不是另外使用一个端口作加密通信。
mail.smtp.starttls.enable: true
mail.smtp.starttls.required: true
mail.smtp.ssl.enable: true
代码
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
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.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Map;
@Service
public class MailService {
@Value("${spring.mail.username}")
private String from;
@Autowired
private JavaMailSender mailSender;
@Autowired
private TemplateEngine templateEngine;
//发送简单文本邮件
public void sendSimpleTextMail(String to, String subject, String content) {
SimpleMailMessage message = new SimpleMailMessage();
message.setTo(to);
message.setSubject(subject);
message.setText(content);
message.setFrom(from);
mailSender.send(message);
}
// 发送 HTML 邮件
public void sendHtmlMail(String to, String subject, String content) throws MessagingException {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper messageHelper = new MimeMessageHelper(message, true);
messageHelper.setFrom(from);
messageHelper.setTo(to);
messageHelper.setSubject(subject);
// true 为 HTML 邮件
messageHelper.setText(content, true);
mailSender.send(message);
}
//发送带附件的邮件
public void sendAttachmentMail(String to, String subject, String content, String... fileArr)
throws MessagingException {
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true);
messageHelper.setFrom(from);
messageHelper.setTo(to);
messageHelper.setSubject(subject);
messageHelper.setText(content, true);
// 添加附件
for (String filePath : fileArr) {
FileSystemResource fileResource = new FileSystemResource(new File(filePath));
if (fileResource.exists()) {
String filename = fileResource.getFilename();
messageHelper.addAttachment(filename, fileResource);
}
}
mailSender.send(mimeMessage);
}
//发送带图片的邮件
public void sendImgMail(String to, String subject, String content, Map<String, String> imgMap)
throws MessagingException {
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true);
messageHelper.setFrom(from);
messageHelper.setTo(to);
messageHelper.setSubject(subject);
messageHelper.setText(content, true);
// 添加图片
for (Map.Entry<String, String> entry : imgMap.entrySet()) {
FileSystemResource fileResource = new FileSystemResource(new File(entry.getValue()));
if (fileResource.exists()) {
messageHelper.addInline(entry.getKey(), fileResource);
}
}
mailSender.send(mimeMessage);
}
//发送模版邮件
public void sendTemplateMail(String to, String subject, Map<String, Object> paramMap, String template)
throws MessagingException {
Context context = new Context();
// 设置变量的值
context.setVariables(paramMap);
String emailContent = templateEngine.process(template, context);
sendHtmlMail(to, subject, emailContent);
}
}
测试类代码
@Test
void contextLoads() throws MessagingException {
mailService.sendSimpleTextMail("xxx@qq.com","你好","在吗");
mailService.sendHtmlMail("xxx@qq.com","你好","<h1>在吗</h1>");
mailService.sendAttachmentMail("xxx@qq.com","你好","在吗","./File/210道面试题.pdf","./File/2021 Java面试真题.pdf");
Map<String,String> map=new HashMap<>();
map.put("img1","./File/微信图片_20190913192041.jpg");
mailService.sendImgMail("xxx@qq.com", "你好", "<h2>Hi~</h2><p>第一封 Springboot HTML 图片邮件</p><br/><img src='cid:img1' />", map);
Map<String, Object> paramMap = new HashMap<>();
paramMap.put("username", "ws6tg");
mailService.sendTemplateMail("xxx@qq.com","你好",paramMap, "templatetest.html");
}
templates模板
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title >邮件</title>
</head>
<body>
<span th:text="${username}"></span>
</body>
</html>