diff --git a/src/main/java/com/example/mathsystemtogether/EmailService.java b/src/main/java/com/example/mathsystemtogether/EmailService.java new file mode 100644 index 0000000..b5f5155 --- /dev/null +++ b/src/main/java/com/example/mathsystemtogether/EmailService.java @@ -0,0 +1,186 @@ +package com.example.mathsystemtogether; + +import jakarta.mail.*; +import jakarta.mail.internet.*; +import java.io.IOException; +import java.io.InputStream; +import java.util.Properties; +import java.util.Random; + +/** + * 邮件服务类 + * 负责发送验证码邮件 + */ +public class EmailService { + + private String smtpHost; + private String smtpPort; + private String fromEmail; + private String fromPassword; + private Session session; + private boolean isConfigured = false; + + public EmailService() { + loadConfiguration(); + if (isConfigured) { + initializeSession(); + } + } + + /** + * 从配置文件加载邮件配置 + */ + private void loadConfiguration() { + try (InputStream input = getClass().getClassLoader().getResourceAsStream("mail.properties")) { + if (input == null) { + return; + } + + Properties props = new Properties(); + props.load(input); + + smtpHost = props.getProperty("mail.smtp.host"); + smtpPort = props.getProperty("mail.smtp.port"); + fromEmail = props.getProperty("mail.from.email"); + fromPassword = props.getProperty("mail.from.password"); + + // 检查配置是否完整 + if (smtpHost != null && smtpPort != null && fromEmail != null && fromPassword != null) { + isConfigured = true; + } + + } catch (IOException e) { + // 配置加载失败,静默处理 + } + } + + /** + * 初始化邮件会话 + */ + private void initializeSession() { + if (!isConfigured) { + return; + } + + Properties props = new Properties(); + props.put("mail.smtp.host", smtpHost); + props.put("mail.smtp.port", smtpPort); + props.put("mail.smtp.auth", "true"); + props.put("mail.smtp.starttls.enable", "true"); + props.put("mail.smtp.ssl.trust", smtpHost); + + // 创建认证器 + Authenticator authenticator = new Authenticator() { + @Override + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication(fromEmail, fromPassword); + } + }; + + session = Session.getInstance(props, authenticator); + } + + /** + * 生成6位数字验证码 + */ + public String generateVerificationCode() { + Random random = new Random(); + return String.format("%06d", random.nextInt(1000000)); + } + + /** + * 发送验证码邮件 + * @param toEmail 接收方邮箱 + * @param verificationCode 验证码 + * @return 发送是否成功 + */ + public boolean sendVerificationCode(String toEmail, String verificationCode) { + if (!isConfigured) { + return false; + } + + try { + Message message = new MimeMessage(session); + message.setFrom(new InternetAddress(fromEmail)); + message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail)); + message.setSubject("数学考试系统 - 邮箱验证码"); + + // 创建邮件内容 + String htmlContent = createEmailContent(verificationCode); + message.setContent(htmlContent, "text/html; charset=UTF-8"); + + // 发送邮件 + Transport.send(message); + return true; + + } catch (MessagingException e) { + return false; + } + } + + /** + * 创建邮件HTML内容 + */ + private String createEmailContent(String verificationCode) { + StringBuilder html = new StringBuilder(); + html.append(""); + html.append(""); + html.append("
"); + html.append(""); + html.append("邮箱验证码
"); + html.append("您正在注册数学考试系统,请使用以下验证码完成注册:
"); + html.append("验证码:
"); + html.append("重要提示:
"); + html.append("如果您没有注册数学考试系统,请忽略此邮件。
"); + html.append("祝您使用愉快!
"); + html.append("数学考试系统团队
"); + html.append("