关键的Maven依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<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>
<dependency>
<groupId>com.icegreen</groupId>
<artifactId>greenmail</artifactId>
<version>1.5.0</version>
<scope>test</scope>
</dependency>
</dependencies>
application.yml(以126邮箱为例)
spring:
mail:
host: smtp.126.com
port: 25
protocol: smtp
defaultEncoding: UTF-8
username: YOUR_EMAIL@126.com
password: YOUR_PASSWORD
核心代码
import org.springframework.beans.factory.annotation.Autowired;
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;
@Service
public class EmailService {
@Autowired
private TemplateEngine templateEngine;
@Autowired
private JavaMailSender javaMailSender;
public String sendMail() throws MessagingException {
Map<String, Object> variables = new HashMap<>();
variables.put("name", "小明");
variables.put("age", 28);
Context context = new Context();
context.setVariables(variables);
String textHtml = templateEngine.process("emails/welcome", context);
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
// 若发生附件,第二个参数必须是true
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
// 这里一定要和配置文件里的username值一样才行
helper.setFrom("YOUR_EMAIL@126.com");
helper.setSubject("邮件主题");
// 若发送HTML格式,第二个参数必须是true
helper.setText(textHtml, true);
helper.setTo("some@qq.com");
helper.addAttachment("鲁棒图.jpg", new File("D:\\鲁棒图.jpg"));
helper.addAttachment("鲁棒图原则.jpg", new File("D:\\鲁棒图原则.jpg"));
javaMailSender.send(mimeMessage);
return "Sent";
}
}
templates/emails/welcome.html
<!doctype html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Email</title>
</head>
<body>
<div>Welcome <b th:text="${name}"></b></div>
<div> Your age is <b th:text="${age}"></b></div>
</body>
</html>
参考资料
- https://mkyong.com/spring-boot/spring-boot-how-to-send-email-via-smtp/
- http://dolszewski.com/spring/sending-html-mail-with-spring-boot-and-thymeleaf/
- https://springhow.com/spring-boot-email-thymeleaf/