package com.nq.utils.email;
|
|
import javax.mail.*;
|
import javax.mail.internet.InternetAddress;
|
import javax.mail.internet.MimeMessage;
|
import java.security.SecureRandom;
|
import java.util.Properties;
|
|
/**
|
* @program: dabaogp
|
* @description:
|
* @create: 2025-09-16 13:44
|
**/
|
public class GmailSender {
|
|
public static void sendEmail(String to, String subject, String body) {
|
// 发件人Gmail地址
|
String from = "coinzne.com@gmail.com";
|
// 使用应用专用密码,而不是常规的Gmail密码
|
String password = "pqupwyxoqedhxlfq";
|
|
// 设置邮件服务器属性
|
Properties props = new Properties();
|
props.put("mail.smtp.host", "smtp.gmail.com");
|
props.put("mail.smtp.port", "587");
|
props.put("mail.smtp.auth", "true");
|
props.put("mail.smtp.starttls.enable", "true"); // 启用TLS加密
|
|
// 获取Session对象
|
Session session = Session.getInstance(props, new javax.mail.Authenticator() {
|
protected PasswordAuthentication getPasswordAuthentication() {
|
return new PasswordAuthentication(from, password);
|
}
|
});
|
|
try {
|
// 创建MimeMessage对象
|
Message message = new MimeMessage(session);
|
|
// 设置发件人
|
message.setFrom(new InternetAddress(from));
|
|
// 设置收件人
|
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
|
|
// 设置邮件主题
|
message.setSubject(subject);
|
|
// 设置邮件正文
|
message.setText(body);
|
|
// 发送邮件
|
Transport.send(message);
|
|
System.out.println("邮件发送成功!");
|
|
} catch (MessagingException e) {
|
throw new RuntimeException("邮件发送失败: " + e.getMessage(), e);
|
}
|
}
|
|
/**
|
* 生成6位随机数字验证码(使用安全随机数生成器)
|
* @return 6位数字字符串
|
*/
|
public static String generateSecureSixDigitCode() {
|
SecureRandom random = new SecureRandom();
|
int code = random.nextInt(900000) + 100000;
|
return String.valueOf(code);
|
}
|
}
|