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 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 "" + "" + "" + "" + "" + "" + "" + "
" + "
" + "

数学学习软件 - 注册验证码

" + "
" + "
" + "

您好!

" + "

您正在注册数学学习软件账户,验证码如下:

" + "
" + code + "
" + "

验证码有效期为 " + EmailConfig.CODE_EXPIRY_MINUTES + " 分钟,请勿泄露给他人。

" + "

如果这不是您本人的操作,请忽略此邮件。

" + "
" + "" + "
" + "" + ""; } 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> iterator = verificationCodes.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry entry = iterator.next(); if (currentTime - entry.getValue().timestamp > EmailConfig.CODE_EXPIRY_MINUTES * 60 * 1000) { iterator.remove(); } } } }