1. 功能概述
使用JavaMailSender实现普通文本邮件和HMTL邮件发送。
2. MailClient
util 包下:MailClient.java
package com.nowcoder.community.util;
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.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
@Component
public class MailClient {
private static final Logger logger = LoggerFactory.getLogger(MailClient.class);
@Autowired
private JavaMailSender mailSender;
@Value("${spring.mail.username}")
private String from;
public void sendMail(String to, String subject, String content) {
try {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
mailSender.send(helper.getMimeMessage());
} catch (MessagingException e) {
logger.error("Fail to send E-mail!" + e.getMessage());
}
}
}
① 注解@Component
把普通 POJO 实例化到 Spring 容器中
② 需要注入JavaMailSender
③ from 的注解@Value
表示发送方
④ 发送邮件由 JavaMailSender 完成。首先JavaMailSender 的实例 mailSender 通过调用 createMimeMessage() 方法创建MimeMessage 的实例 message,再将 message 装配到 MimeMessageHelper 中生成 MimeMessageHelper 的实例 helper ,通过 helper 设置邮件的发送方、发送对象、标题、正文内容等。最后通过 mailSender 实现邮件发送。
3. MailTests
编写测试类MailTests.java
测试发送不同类型邮件。
package com.nowcoder.community;
import com.nowcoder.community.util.MailClient;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
@SpringBootTest
@ContextConfiguration(classes = CommunityApplication.class)
public class MailTests {
@Autowired
private MailClient mailClient;
@Autowired
private TemplateEngine templateEngine;
@Test
public void testTextMail() {
mailClient.sendMail("xxx@sina.com", "TEST", "Welcome to Sina!");
}
@Test
public void testHtmlMail() {
Context context = new Context();
context.setVariable("username", "Lebron Le");
String content = templateEngine.process("/mail/demo", context);
System.out.println(content);
mailClient.sendMail("xxx@sina.com", "HTML", content);
}
}
① 需要注入MailClient
、TemplateEngine
@ 通过模板引擎生成 html 邮件
③ context.setVariable("username", "Lebron Le");
用于动态替换 html 文件中的对应拼接符
4. HTML 模板文件
templates/mail/demo.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>邮件示例</title>
</head>
<body>
<p>欢迎您,<span style="color:red;" th:text="${username}"></span>!</p>
</body>
</html>
5. 邮箱配置
新浪邮箱需要打开“客户端授权码”、“POP3/SMTP”服务、“IMAP4服务/SMTP服务”,同时设置:
application.properties
# MailProperties
spring.mail.host=smtp.sina.com
spring.mail.port=465
spring.mail.username=xxx@sina.com
spring.mail.password=客户端授权码,不是登录邮箱的密码
spring.mail.protocol=smtps
spring.mail.default-encoding=UTF-8
spring.mail.properties.mail.smtp.ssl.enable=true
spring.mail.properties.mail.smtp.socketFactory.port=465
spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
6. 发送结果
① 发送了一封标题为 TEST,正文内容为 Welcome to Sina! 的普通文本邮件。
② 发送了一封标题为 HTML,正文内容为 欢迎您,Lebron Le!的 HTML 邮件。