|
|
package com.ybw.mathapp.service;
|
|
|
|
|
|
import com.ybw.mathapp.config.EmailConfig;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Iterator;
|
|
|
import java.util.Map;
|
|
|
import java.util.Map.Entry;
|
|
|
import java.util.Properties;
|
|
|
import java.util.Random;
|
|
|
//import javax.mail.*;
|
|
|
//import javax.mail.internet.*;
|
|
|
|
|
|
public class EmailService {
|
|
|
private static Map<String, VerificationCodeInfo> verificationCodes = new HashMap<>();
|
|
|
|
|
|
private static class VerificationCodeInfo {
|
|
|
String code;
|
|
|
long timestamp;
|
|
|
|
|
|
VerificationCodeInfo(String code, long timestamp) {
|
|
|
this.code = code;
|
|
|
this.timestamp = timestamp;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public static String generateVerificationCode() {
|
|
|
Random random = new Random();
|
|
|
int code = 100000 + random.nextInt(900000);
|
|
|
return String.valueOf(code);
|
|
|
}
|
|
|
|
|
|
public static boolean sendVerificationCode(String recipientEmail, String code) {
|
|
|
/*
|
|
|
try {
|
|
|
// 创建邮件会话
|
|
|
Properties props = new Properties();
|
|
|
props.put("mail.smtp.host", EmailConfig.SMTP_HOST);
|
|
|
props.put("mail.smtp.port", EmailConfig.SMTP_PORT);
|
|
|
props.put("mail.smtp.auth", "true");
|
|
|
props.put("mail.smtp.starttls.enable", "true");
|
|
|
|
|
|
// 创建认证器
|
|
|
Authenticator auth = new Authenticator() {
|
|
|
protected PasswordAuthentication getPasswordAuthentication() {
|
|
|
return new PasswordAuthentication(
|
|
|
EmailConfig.SENDER_EMAIL,
|
|
|
EmailConfig.SENDER_PASSWORD
|
|
|
);
|
|
|
}
|
|
|
};
|
|
|
|
|
|
Session session = Session.getInstance(props, auth);
|
|
|
|
|
|
// 创建邮件消息
|
|
|
Message message = new MimeMessage(session);
|
|
|
message.setFrom(new InternetAddress(EmailConfig.SENDER_EMAIL));
|
|
|
message.setRecipients(Message.RecipientType.TO,
|
|
|
InternetAddress.parse(recipientEmail));
|
|
|
message.setSubject(EmailConfig.EMAIL_SUBJECT);
|
|
|
|
|
|
// 创建邮件内容
|
|
|
String emailContent = createEmailContent(code);
|
|
|
message.setContent(emailContent, "text/html; charset=utf-8");
|
|
|
|
|
|
// 发送邮件
|
|
|
Transport.send(message);
|
|
|
|
|
|
// 存储验证码信息
|
|
|
verificationCodes.put(recipientEmail,
|
|
|
new VerificationCodeInfo(code, System.currentTimeMillis()));
|
|
|
|
|
|
System.out.println("验证码已发送到邮箱: " + recipientEmail);
|
|
|
return true;
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
System.err.println("发送邮件失败: " + e.getMessage());
|
|
|
// 在开发环境中,直接返回true用于测试
|
|
|
verificationCodes.put(recipientEmail,
|
|
|
new VerificationCodeInfo(code, System.currentTimeMillis()));
|
|
|
return true;
|
|
|
}
|
|
|
*/
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
private static String createEmailContent(String code) {
|
|
|
return "<!DOCTYPE html>" +
|
|
|
"<html>" +
|
|
|
"<head>" +
|
|
|
"<meta charset='UTF-8'>" +
|
|
|
"<style>" +
|
|
|
"body { font-family: Arial, sans-serif; line-height: 1.6; color: #333; }" +
|
|
|
".container { max-width: 600px; margin: 0 auto; padding: 20px; }" +
|
|
|
".header { background-color: #4CAF50; color: white; padding: 20px; text-align: center; }" +
|
|
|
".content { padding: 20px; background-color: #f9f9f9; margin: 20px 0; }" +
|
|
|
".code { font-size: 24px; font-weight: bold; color: #4CAF50; text-align: center; padding: 15px; background-color: white; border: 2px dashed #4CAF50; margin: 20px 0; }" +
|
|
|
".footer { text-align: center; color: #666; font-size: 12px; }" +
|
|
|
"</style>" +
|
|
|
"</head>" +
|
|
|
"<body>" +
|
|
|
"<div class='container'>" +
|
|
|
"<div class='header'>" +
|
|
|
"<h2>数学学习软件 - 注册验证码</h2>" +
|
|
|
"</div>" +
|
|
|
"<div class='content'>" +
|
|
|
"<p>您好!</p>" +
|
|
|
"<p>您正在注册数学学习软件账户,验证码如下:</p>" +
|
|
|
"<div class='code'>" + code + "</div>" +
|
|
|
"<p>验证码有效期为 " + EmailConfig.CODE_EXPIRY_MINUTES + " 分钟,请勿泄露给他人。</p>" +
|
|
|
"<p>如果这不是您本人的操作,请忽略此邮件。</p>" +
|
|
|
"</div>" +
|
|
|
"<div class='footer'>" +
|
|
|
"<p>此邮件为系统自动发送,请勿回复。</p>" +
|
|
|
"</div>" +
|
|
|
"</div>" +
|
|
|
"</body>" +
|
|
|
"</html>";
|
|
|
}
|
|
|
|
|
|
public static boolean verifyCode(String email, String inputCode) {
|
|
|
VerificationCodeInfo codeInfo = verificationCodes.get(email);
|
|
|
if (codeInfo == null) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
// 检查验证码是否过期
|
|
|
long currentTime = System.currentTimeMillis();
|
|
|
if (currentTime - codeInfo.timestamp > EmailConfig.CODE_EXPIRY_MINUTES * 60 * 1000) {
|
|
|
verificationCodes.remove(email);
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
return codeInfo.code.equals(inputCode);
|
|
|
}
|
|
|
|
|
|
public static void cleanupExpiredCodes() {
|
|
|
long currentTime = System.currentTimeMillis();
|
|
|
Iterator<Entry<String, VerificationCodeInfo>> iterator =
|
|
|
verificationCodes.entrySet().iterator();
|
|
|
|
|
|
while (iterator.hasNext()) {
|
|
|
Map.Entry<String, VerificationCodeInfo> entry = iterator.next();
|
|
|
if (currentTime - entry.getValue().timestamp >
|
|
|
EmailConfig.CODE_EXPIRY_MINUTES * 60 * 1000) {
|
|
|
iterator.remove();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
} |