一、首先到QQ邮箱申请开启POP3、SMTP协议
二、安装依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.3.23</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
三、配置文件
spring:
mail:
host: smtp.qq.com
username: [email protected]
password: xxxxxx
port: 587 # 对于 TLS 连接使用 587,对于 SSL 连接使用 465
properties:
mail:
smtp:
auth: true
starttls:
enable: true
required: true
debug: true # 启用调试模式以查看详细日志
thymeleaf:
cache: false
prefix: classpath:/templates/
四、如果需要发送模板的邮件,则在/templates/目录下建立email.html文件
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>使用Thymeleaf作为邮件的模板</title>
</head>
<body>
<p>hello 欢迎加入 xxx 大家庭,您的入职信息如下:</p>
<table borde