楼主项目spring boot版本如下
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.2</version>
<relativePath/>
</parent>
1、先开启qq邮箱的POP3/IMAP/SMTP/Exchange/CardDAV 服务
在qq邮箱页面右上角的,点击 账号与安全 来到下面的截图,再点击 安全设置
记录下生成的host、username(就是账号邮箱)、授权码(就是一会写服务时会用到的密码)
2、导入spring-boot-starter-mail Maven依赖
<!--邮箱发送验证码-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
记得刷新一下Maven
3、编写application.yml 配置项
spring:
main:
default-encoding: UTF-8
host: smtp.qq.com
username: 124********@qq.com
password: hv*******bagd
对象步骤1记录下来的三个信息
4、编写MailUtils 工具类
package com.douyin.util;
import jakarta.mail.*;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeMessage;
import org.eclipse.angus.mail.util.MailSSLSocketFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.security.GeneralSecurityException;
import java.util.Properties;
@Component
public class MailUtils {
private static String host;
private static String username;
private static String password;
@Value("${spring.main.host}")
public void setHost(String host) {
MailUtils.host = host;
}
@Value("${spring.main.username}")
public void setUsername(String username) {
MailUtils.username = username;
}
@Value("${spring.main.password}")
public void setPassword(String password) {
MailUtils.password = password;
}
public static void sendMail(String to, String vcode) throws GeneralSecurityException {
Properties props = System.getProperties();
props.setProperty("mail.smtp.host", host);
props.put("mail.smtp.auth", "true");
//SSL加密
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.ssl.socketFactory", sf);
Session session = Session.getDefaultInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
//这个用户名密码就可以登录到邮箱服务器了,用它给别人发送邮件
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
//设置发件人:
message.setFrom(new InternetAddress(username));
//设置收件人 这个TO就是收件人
message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
//邮件的主题
message.setSubject("douyin_project夏川优佳注册验证码");
message.setContent("<h1>来自douyin_project视频网站的注册用户验证码邮件,请接收你的验证码:</h1><h3>你的验证码是:" + vcode + ",请妥善保管好你的验证码!</h3>"+"<h3>5分钟内有效!</h3>", "text/html;charset=UTF-8");
Transport.send(message);
} catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
5、使用工具类发送邮件
@Service
public class LoginServiceImpl implements LoginService {
@Override
public void test(String email) {
//生成随机6位数字验证码
String emailCode = getSixCode();
//email为收到邮件人的qq邮箱号
MailUtils.sendMail(email, emailCode);
}
public static String getSixCode() {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < 6; i++) {
int code = (int) (Math.random() * 10);
builder.append(code);
}
return builder.toString();
}
}