You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
double_project/src/utils/EmailUtil.java

111 lines
4.8 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package utils;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
import java.util.Random;
public class EmailUtil {
private static final Random random = new Random();
// SMTP服务器配置 - 请修改为您的邮箱配置
private static final String SMTP_HOST = "smtp.qq.com"; // QQ邮箱SMTP
private static final String SMTP_PORT = "587"; // 端口
private static final String FROM_EMAIL = "2536082954@qq.com"; // 发件邮箱
private static final String EMAIL_PASSWORD = "uihuasdsosbvdiia"; // 授权码
private static final String FROM_NAME = "数学学习软件";
public static String generateRegistrationCode() {
return String.format("%06d", random.nextInt(1000000));
}
/**
* 发送注册码到用户输入的邮箱
*/
public static boolean sendRegistrationCode(String userEmail, String username, String code) {
// 配置邮件服务器
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", SMTP_HOST);
props.put("mail.smtp.port", SMTP_PORT);
// 超时设置
props.put("mail.smtp.timeout", "10000");
props.put("mail.smtp.connectiontimeout", "10000");
// 创建认证器
Authenticator authenticator = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(FROM_EMAIL, EMAIL_PASSWORD);
}
};
Session session = Session.getInstance(props, authenticator);
try {
// 创建邮件
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(FROM_EMAIL, FROM_NAME, "UTF-8"));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(userEmail));
message.setSubject("数学学习软件 - 注册验证码", "UTF-8");
// 邮件内容HTML格式
String htmlContent = buildEmailContent(username, code);
message.setContent(htmlContent, "text/html;charset=UTF-8");
// 发送邮件
Transport.send(message);
System.out.println("✅ 注册码已成功发送到: " + userEmail);
return true;
} catch (Exception e) {
System.err.println("❌ 邮件发送失败: " + e.getMessage());
e.printStackTrace();
return false;
}
}
/**
* 构建邮件内容
*/
private static String buildEmailContent(String username, String code) {
return "<!DOCTYPE html>" +
"<html>" +
"<head>" +
" <meta charset=\"UTF-8\">" +
" <style>" +
" body { font-family: Arial, sans-serif; margin: 0; padding: 20px; background-color: #f5f5f5; }" +
" .container { max-width: 600px; margin: 0 auto; background: white; padding: 30px; border-radius: 10px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); }" +
" .header { color: #1890ff; text-align: center; margin-bottom: 30px; }" +
" .code-box { background: #f0f8ff; padding: 20px; text-align: center; margin: 25px 0; border-radius: 8px; border: 2px dashed #1890ff; }" +
" .code { font-size: 32px; font-weight: bold; color: #ff4d4f; letter-spacing: 5px; }" +
" .footer { margin-top: 30px; padding-top: 20px; border-top: 1px solid #eee; color: #666; font-size: 12px; }" +
" </style>" +
"</head>" +
"<body>" +
" <div class=\"container\">" +
" <div class=\"header\">" +
" <h1>数学学习软件 - 注册验证码</h1>" +
" </div>" +
" <p>亲爱的 <strong>" + username + "</strong>,您好!</p>" +
" <p>您正在注册数学学习软件,请使用以下验证码完成注册:</p>" +
" <div class=\"code-box\">" +
" <div class=\"code\">" + code + "</div>" +
" </div>" +
" <p>如果这不是您的操作,请忽略此邮件。</p>" +
" <div class=\"footer\">" +
" <p>此为系统邮件,请勿回复</p>" +
" <p>数学学习软件团队</p>" +
" </div>" +
" </div>" +
"</body>" +
"</html>";
}
}