1.导入pom依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.7</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>top.litongfei</groupId>
<artifactId>spring-boot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot</name>
<description>spring-boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
2.yml加入配置,password就是授权码
#server:
# port: 8888
#spring:
# mail:
# default-encoding: utf-8
# host: smtp.qq.com
# username: 2389161011@qq.com
# password:
# properties:
# mail:
# smtp:
# auth: true
# port: 465
# ssl:
# enable: true
server:
port: 8888
spring:
mail:
default-encoding: utf-8
host: smtp.163.com
username: 15068788040@163.com
password:
properties:
mail:
smtp:
auth: true
port: 465
ssl:
enable: true
3.thymeleaf模板代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div>
姓名: <input type="text" th:value="${name}"/> <br>
岗位: <input type="text" th:value="${posName}"/> <br>
职级: <input type="text" th:value="${joblevelName}"/> <br>
部门: <input type="text" th:value="${departmentName}"/> <br>
</div>
</body>
</html>
4.java调用
package top.litongfei.springboot.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Date;
@RestController
public class MailController {
@Autowired
TemplateEngine templateEngine;
@Autowired
JavaMailSender javaMailSender;
@RequestMapping("/send/simpleMail")
public void sendSimpleMail() {
//构建邮件对象
SimpleMailMessage message = new SimpleMailMessage();
//邮件主题
message.setSubject("这是一封测试邮件");
//邮件发送者
message.setFrom("15068788040@163.com");
//邮件接收者(可以有多个)
message.setTo("tongfei.li@coolcollege.cn");;
//邮件发送日期
message.setSentDate(new Date());
//邮件正文
message.setText("这是测试邮件的正文");
//发送邮件
javaMailSender.send(message);
}
@RequestMapping("/send/thymeleafMail")
public void sendThymeleafMail() throws MessagingException {
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage);
mimeMessageHelper.setSubject("入职欢迎");
mimeMessageHelper.setSentDate(new Date());
mimeMessageHelper.setFrom("15068788040@163.com");
mimeMessageHelper.setTo("tongfei.li@coolcollege.cn");
Context context = new Context();
context.setVariable("name", "张二");
context.setVariable("posName", "销售");
context.setVariable("joblevelName","销售总监");
context.setVariable("departmentName","销售部" );
String text = templateEngine.process("person.html", context);
mimeMessageHelper.setText(text,true);
javaMailSender.send(mimeMessage);
}
}