多樣化郵件功能實戰(zhàn)指南,你學會了嗎?
前言
在當今數(shù)字化的時代,郵件作為一種重要的通信方式,廣泛應(yīng)用于各類系統(tǒng)中。無論是系統(tǒng)通知、用戶交互,還是文件傳輸?shù)葓鼍?,郵件都發(fā)揮著不可或缺的作用。
本文將探討如何實現(xiàn)文本、附件、HTML、圖片類型郵件的發(fā)送,并在此基礎(chǔ)上增加一些實用功能,如批量發(fā)送郵件、動態(tài)郵件模板渲染等,助力開發(fā)者打造更強大的郵件服務(wù)。
實現(xiàn)
依賴引入
<dependencies>
<!-- Spring Boot Web支持,用于后續(xù)可能的Web接口開發(fā) -->
<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替換為郵箱的授權(quán)碼(非登錄密碼)。若使用其他郵箱服務(wù)器,需相應(yīng)修改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);
}
實現(xiàn)類
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("文本郵件已經(jīng)發(fā)送");
} catch (Exception e) {
log.error("發(fā)生發(fā)送文本郵件錯誤!", 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郵件發(fā)送成功");
} catch (MessagingException e) {
log.error("發(fā)生發(fā)送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("帶附件的郵件已經(jīng)發(fā)送");
} catch (MessagingException e) {
log.error("發(fā)生發(fā)送帶附件郵件錯誤!", 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("嵌入靜態(tài)圖片的郵件已經(jīng)發(fā)送");
} catch (MessagingException e) {
log.error("發(fā)生發(fā)送嵌入靜態(tài)圖片郵件錯誤!", e);
}
}
@Override
public void sendBatchSimpleMail(String[] tos, String subject, String content) {
for (String to : tos) {
sendSimpleMail(to, subject, content);
}
log.info("批量文本郵件已發(fā)送完成");
}
@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("轉(zhuǎn)換對象為Map時出錯", e);
}
}
context.setVariables(map);
}
String emailContent = templateEngine.process(templateName, context);
sendHtmlMail(to, subject, emailContent);
log.info("動態(tài)模板郵件已發(fā)送");
}
}
在 src/main/resources/templates 目錄下創(chuàng)建 userInfoTemplate.html 文件(Thymeleaf 默認會從該目錄加載模板),示例內(nèi)容如下:
<!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, "批量測試郵件", "這是批量發(fā)送的文本郵件");
}
@Test
public void sendDynamicTemplateMail() {
User user = new User("一安", 25);
mailService.sendDynamicTemplateMail("test@example.com", "動態(tài)模板郵件測試", "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方法
}
}
總結(jié)
在實際項目中,還可以進一步拓展郵件服務(wù)的功能,例如:
- 異步發(fā)送郵件:使用 Spring 的異步任務(wù)機制,將郵件發(fā)送任務(wù)異步化,避免阻塞主線程,提高系統(tǒng)性能和響應(yīng)速度。
- 郵件發(fā)送狀態(tài)跟蹤:通過郵件服務(wù)器的反饋或自定義的跟蹤機制,記錄郵件的發(fā)送狀態(tài)(如發(fā)送成功、失敗、已讀等),方便系統(tǒng)進行后續(xù)處理。