前言
在当今数字化的时代,邮件作为一种重要的通信方式,广泛应用于各类系统中。无论是系统通知、用户交互,还是文件传输等场景,邮件都发挥着不可或缺的作用。
本文将探讨如何实现文本、附件、HTML、图片类型邮件的发送,并在此基础上增加一些实用功能,如批量发送邮件、动态邮件模板渲染等,助力开发者打造更强大的邮件服务。
实现
依赖引入
<dependencies>
<!-- Spring Boot Web支持,用于后续可能的Web接口开发 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Boot邮件启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<!-- JavaMail API -->
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
</dependency>
<!-- Thymeleaf模板引擎,用于邮件模板渲染 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- 测试依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Lombok简化代码编写 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
配置信息
spring:
mail:
host: smtp.163.com
port: 465
username: your_email@163.com
password: your_password
properties:
mail:
debug: true
smtp:
auth: true
starttls.enable: true
socketFactoryClass: javax.net.ssl.SSLSocketFactory
default-encoding: UTF-8
protocol: smtps
thymeleaf:
prefix: classpath:/templates/
suffix: .html
cache: false
from:
mail:
address: your_email@163.com
请将your_email@163.com替换为实际的邮箱地址,your_password替换为邮箱的授权码(非登录密码)。若使用其他邮箱服务器,需相应修改spring.mail.host等配置。
核心代码
public interface MailService {
void sendSimpleMail(String to, String subject, String content);
void sendHtmlMail(String to, String subject, String content);
void sendAttachmentsMail(String to, String subject, String content, String filePath);
void sendInlineResourceMail(String to, String subject, String content, String rscPath, String rscId);
void sendBatchSimpleMail(String[] tos, String subject, String content);
void sendDynamicTemplateMail(String to, String subject, String templateName, Object model);
}
实现类
import lombok.extern.slf4j.Slf4j;
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 org.yian.service.MailService;
import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
@Service
@Slf4j
public class MailServiceImpl implements MailService {
@Resource
private JavaMailSender mailSender;
@Resource
private TemplateEngine templateEngine;
@Value("${from.mail.address}")
private String from;
@Override
public void sendSimpleMail(String to, String subject, String content) {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(from);
message.setTo(to);
message.setSubject(subject);
message.setText(content);
try {
mailSender.send(message);
log.info("文本邮件已经发送");
} catch (Exception e) {
log.error("发生发送文本邮件错误!", e);
}
}
@Override
public void sendHtmlMail(String to, String subject, String content) {
MimeMessage message = mailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
mailSender.send(message);
log.info("html邮件发送成功");
} catch (MessagingException e) {
log.error("发生发送html邮件错误!", e);
}
}
@Override
public void sendAttachmentsMail(String to, String subject, String content, String filePath) {
MimeMessage message = mailSender.createMimeMessage();
try {
MimeMessageHelper 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 = filePath.substring(filePath.lastIndexOf(File.separator));
helper.addAttachment(fileName, file);
mailSender.send(message);
log.info("带附件的邮件已经发送");
} catch (MessagingException e) {
log.error("发生发送带附件邮件错误!", e);
}
}
@Override
public void sendInlineResourceMail(String to, String subject, String content, String rscPath, String rscId) {
MimeMessage message = mailSender.createMimeMessage();
try {
MimeMessageHelper 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);
log.info("嵌入静态图片的邮件已经发送");
} catch (MessagingException e) {
log.error("发生发送嵌入静态图片邮件错误!", e);
}
}
@Override
public void sendBatchSimpleMail(String[] tos, String subject, String content) {
for (String to : tos) {
sendSimpleMail(to, subject, content);
}
log.info("批量文本邮件已发送完成");
}
@Override
public void sendDynamicTemplateMail(String to, String subject, String templateName, Object model) {
Context context = new Context();
if (model instanceof Map) {
context.setVariables((Map<String, Object>) model);
} elseif (model != null) {
Map<String, Object> map = new HashMap<>();
Field[] fields = model.getClass().getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
try {
map.put(field.getName(), field.get(model));
} catch (IllegalAccessException e) {
log.error("转换对象为Map时出错", e);
}
}
context.setVariables(map);
}
String emailContent = templateEngine.process(templateName, context);
sendHtmlMail(to, subject, emailContent);
log.info("动态模板邮件已发送");
}
}
在 src/main/resources/templates 目录下创建 userInfoTemplate.html 文件(Thymeleaf 默认会从该目录加载模板),示例内容如下:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>用户信息模板</title>
</head>
<body>
<h1>用户信息</h1>
<p>姓名:<span th:text="${name}"></span></p>
<p>年龄:<span th:text="${age}"></span></p>
</body>
</html>
单元测试
import cn.example.mail.service.MailService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class MailBootTest {
@Autowired
private MailService mailService;
@Test
public void testSimpleMail() {
mailService.sendSimpleMail("test@example.com", "测试简单文本邮件", "这是一封简单的文本邮件");
}
@Test
public void testHtmlMail() {
String content = "<html><body><h2>hello! 这是一封html邮件!</h2></body></html>";
mailService.sendHtmlMail("test@example.com", "这是html邮件", content);
}
@Test
public void sendAttachmentsMail() {
String filePath = "C:\\example\\attachment.pdf";
mailService.sendAttachmentsMail("test@example.com", "主题:带附件的邮件", "有附件,请查收!", filePath);
}
@Test
public void sendInlineResourceMail() {
String rscId = "exampleImage";
String content = "<html><body>这是有图片的邮件:<img src='cid:" + rscId + "'></body></html>";
String imgPath = "C:\\example\\image.jpg";
mailService.sendInlineResourceMail("test@example.com", "主题:这是有图片的邮件", content, imgPath, rscId);
}
@Test
public void sendBatchSimpleMail() {
String[] tos = {"test1@example.com", "test2@example.com"};
mailService.sendBatchSimpleMail(tos, "批量测试邮件", "这是批量发送的文本邮件");
}
@Test
public void sendDynamicTemplateMail() {
User user = new User("一安", 25);
mailService.sendDynamicTemplateMail("test@example.com", "动态模板邮件测试", "userInfoTemplate", user);
}
// 测试用的用户类
private static class User {
private String name;
private int age;
public User(String name, int age) {
this.name = name;
this.age = age;
}
// 省略getter和setter方法
}
}
总结
在实际项目中,还可以进一步拓展邮件服务的功能,例如:
- 异步发送邮件:使用 Spring 的异步任务机制,将邮件发送任务异步化,避免阻塞主线程,提高系统性能和响应速度。
- 邮件发送状态跟踪:通过邮件服务器的反馈或自定义的跟踪机制,记录邮件的发送状态(如发送成功、失败、已读等),方便系统进行后续处理。