1.qq邮箱开启对应服务
![](https://img-blog.csdnimg.cn/direct/18c6987e68d2434ba0499404c47fced9.png)
![](https://img-blog.csdnimg.cn/direct/334b9f59aa8a4099a112d67622207ef6.png)
![](https://img-blog.csdnimg.cn/direct/25b2f08f818842b78800a8d394e9591b.png)
2.引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
3.封装工具类及配置文件
![](https://img-blog.csdnimg.cn/direct/1e83a10e90994e9e8d6e29f9d7ab007a.png)
package com.common.util;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
/**
* 发邮件工具类
*/
@Component
public final class MailUtils {
@Value("${spring:mail:username}")
private String USER; // 发件人邮箱地址
@Value("${spring:mail:password}")
private String PASSWORD; // 如果是qq邮箱可以使户端授权码
/**
* 发送邮件
* @param to 收件人邮箱
* @param text 邮件正文
* @param title 标题
* mail:
* password: ennnntqxzijsdjdh
* username: 2413500195@qq.com
* host: smtp.qq.com
* default-encoding: UTF-8
*/
public boolean sendMail(String to, String text, String title){
try {
final Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.qq.com");
// 发件人的账号
props.put("spring:mail:username", USER);
//发件人的密码
props.put("spring:mail:password", PASSWORD);
// 构建授权信息,用于进行SMTP进行身份验证
Authenticator authenticator = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
// 用户名、密码
String userName = props.getProperty("spring:mail:username");
String password = props.getProperty("spring:mail:password");
return new PasswordAuthentication(userName, password);
}
};
// 使用环境属性和授权信息,创建邮件会话
Session mailSession = Session.getInstance(props, authenticator);
// 创建邮件消息
MimeMessage message = new MimeMessage(mailSession);
// 设置发件人
String username = props.getProperty("spring:mail:username");
InternetAddress form = new InternetAddress(username);
message.setFrom(form);
// 设置收件人
InternetAddress toAddress = new InternetAddress(to);
message.setRecipient(Message.RecipientType.TO, toAddress);
// 设置邮件标题
message.setSubject(title);
// 设置邮件的内容体
message.setContent(text, "text/html;charset=UTF-8");
// 发送邮件
Transport.send(message);
return true;
}catch (Exception e){
e.printStackTrace();
}
return false;
}
}