1. pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
2.application.properties
spring.mail.host=smtp.qq.com
spring.mail.username=yourAccount@qq.com
spring.mail.password=yourPassword
spring.mail.default-encoding=UTF-8
3. 开启邮箱第三方支持以及获取授权码(以QQ邮箱为例)

4. 代码编写
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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 javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
@Service
public class MailUtils {
private final Logger logger = LoggerFactory.getLogger(MailUtils.class);
@Value("${spring.mail.username}")
private String from;
@Autowired
private JavaMailSender mailSender;
public void sendSimpleMail(
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);
}
public void sendHtmlMail(
String to, String subject, String content
) {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper;
try {
helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
mailSender.send(message);
logger.info("发送HTML邮件成功");
} catch (MessagingException e) {
e.printStackTrace();
logger.error("发送HTML邮件失败:", e);
}
}
public void sendAttachmentMail(
String to, String subject,
String content, String filePath
) {
logger.info("发送带附件邮件开始:{},{},{},{}", to, subject, content, filePath);
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper;
try {
helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
FileSystemResource file = new FileSystemResource(new File(filePath));
String fileName = file.getFilename();
helper.addAttachment(fileName, file);
mailSender.send(message);
logger.info("发送带附件邮件成功");
} catch (MessagingException e) {
logger.error("发送带附件邮件失败", e);
}
}
public void sendInlineResourceMail(
String to, String subject, String content,
String rscPath, String rscId) {
logger.info("发送带图片邮件开始:{},{},{},{},{}", to, subject, content, rscPath, rscId);
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper;
try {
helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
FileSystemResource res = new FileSystemResource(new File(rscPath));
helper.addInline(rscId, res);
mailSender.send(message);
logger.info("发送带图片邮件成功");
} catch (MessagingException e) {
logger.error("发送带图片邮件失败", e);
}
}
}
5. 测试
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootApplicationTest {
@Autowired
MailUtils mailUtils;
@Test
public void sendSimpleMail() {
mailUtils.sendSimpleMail("zhaokuii11@163.com", "发送邮件测试", "大家好,这是我用springboot进行发送邮件测试");
}
@Test
public void sendHtmlMail() {
String content = "<html><body><h3><font color=\"red\">" + "大家好,这是springboot发送的HTML邮件" + "</font></h3></body></html>";
mailUtils.sendHtmlMail("zhaokuii11@163.com", "发送邮件测试", content);
}
@Test
public void sendAttachmentMail() {
String content = "<html><body><h3><font color=\"red\">" + "大家好,这是springboot发送的HTML邮件,有附件哦" + "</font></h3></body></html>";
String filePath = "your file path";
mailUtils.sendAttachmentMail("zhaokuii11@163.com", "发送邮件测试", content, filePath);
}
@Test
public void sendInlineResourceMail() {
String rscPath = "your picture path";
String rscId = "001";
String content = "<html><body><h3><font color=\"red\">" + "大家好,这是springboot发送的HTML邮件,有图片哦" + "</font></h3>"
+ "<img src=\'cid:" + rscId + "\'></body></html>";
mailUtils.sendInlineResourceMail("zhaokuii11@163.com", "发送邮件测试", content, rscPath, rscId);
}
}
6. 发送模板邮件
- 以Thymeleaf模板引擎为例来讲解(具体语法自行了解)
6.0 导入 thymeleaf依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
6.1 在templates文件夹下创建emailTemplate.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>邮件模板</title>
</head>
<body>
您好,感谢您的注册,这是一封验证邮件,请点击下面的链接完成注册,感谢您的支持!<br>
<a href="#" th:href="@{http://www.bestbpf.com/register/{id}(id=${id})}">激活账户</a>
</body>
</html>
6.2 测试
@Autowired
private TemplateEngine templateEngine;
@Autowired
MailUtils mailUtils;
@Test
public void testTemplateMail() {
Context context = new Context();
context.setVariable("id", "001");
String emailContent = templateEngine.process("emailTemplate", context);
mailUtils.sendHtmlMail("zhaokuii11@163.com", "这是一个模板文件", emailContent);
}
参考
https://www.jianshu.com/p/59d15b357201
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
2021-01-11 网络编程