From 4ebc155285fbe994b7ba4ae12a05e6b417a5ab79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E9=BB=98=E6=B6=B5?= <15530826+wgll926@user.noreply.gitee.com> Date: Sat, 11 Oct 2025 15:13:05 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B3=A8=E5=86=8C=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E5=B9=B6=E5=AE=8C=E5=96=84=E9=82=AE=E7=AE=B1=E9=AA=8C?= =?UTF-8?q?=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mathsystemtogether/EmailService.java | 186 +++++++++++ .../RegisterController.java | 310 ++++++++++++++++++ .../mathsystemtogether/register-view.fxml | 131 ++++++++ src/main/resources/mail.properties | 43 +++ user_data.txt | 1 + 5 files changed, 671 insertions(+) create mode 100644 src/main/java/com/example/mathsystemtogether/EmailService.java create mode 100644 src/main/java/com/example/mathsystemtogether/RegisterController.java create mode 100644 src/main/resources/com/example/mathsystemtogether/register-view.fxml create mode 100644 src/main/resources/mail.properties create mode 100644 user_data.txt 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("