From 6a220404cb3a2af7660ab10a551b0e87894d82fb Mon Sep 17 00:00:00 2001 From: smallbailangui Date: Tue, 7 Oct 2025 21:08:49 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E7=9F=AD=E4=BF=A1?= =?UTF-8?q?=E5=8F=91=E9=80=81=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 +- .../controller/RegisterController.java | 13 ++++-- .../mathgenerator/service/EmailConfig.java | 35 ++++++++++++++++ .../mathgenerator/service/UserService.java | 41 +++++++++++++++---- 4 files changed, 80 insertions(+), 12 deletions(-) create mode 100644 src/main/java/com/mathgenerator/service/EmailConfig.java diff --git a/.gitignore b/.gitignore index 2fa42a8..25cc136 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,5 @@ generated_papers/ target/ users.json legal/ -lib/ \ No newline at end of file +lib/ +config.properties \ No newline at end of file diff --git a/src/main/java/com/mathgenerator/controller/RegisterController.java b/src/main/java/com/mathgenerator/controller/RegisterController.java index c1a2835..0227866 100644 --- a/src/main/java/com/mathgenerator/controller/RegisterController.java +++ b/src/main/java/com/mathgenerator/controller/RegisterController.java @@ -35,10 +35,17 @@ public class RegisterController { statusLabel.setText("请输入一个有效的邮箱地址!"); return; } - // 调用后端服务发送验证码(模拟) + + // 调用后端服务发送验证码 this.sentCode = userService.sendVerificationCode(email); - statusLabel.setText("验证码已发送(请查看控制台输出)。"); - sendCodeButton.setDisable(true); // 防止重复点击 + + // 处理发送结果 + if (this.sentCode != null) { + statusLabel.setText("验证码已成功发送,请查收您的邮箱。"); + sendCodeButton.setDisable(true); // 防止重复点击 + } else { + statusLabel.setText("验证码发送失败!请检查配置或联系管理员。"); + } } @FXML diff --git a/src/main/java/com/mathgenerator/service/EmailConfig.java b/src/main/java/com/mathgenerator/service/EmailConfig.java new file mode 100644 index 0000000..44618f8 --- /dev/null +++ b/src/main/java/com/mathgenerator/service/EmailConfig.java @@ -0,0 +1,35 @@ +package com.mathgenerator.service; + +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.Properties; + +public class EmailConfig { + private static final Properties properties = new Properties(); + + static { + try (InputStream input = new FileInputStream("config.properties")) { + properties.load(input); + } catch (IOException ex) { + System.err.println("错误:无法加载 config.properties 文件!请确保该文件在项目根目录中。"); + ex.printStackTrace(); + } + } + + public static String getHost() { + return properties.getProperty("smtp.host"); + } + + public static int getPort() { + return Integer.parseInt(properties.getProperty("smtp.port")); + } + + public static String getUsername() { + return properties.getProperty("smtp.username"); + } + + public static String getPassword() { + return properties.getProperty("smtp.password"); + } +} \ No newline at end of file diff --git a/src/main/java/com/mathgenerator/service/UserService.java b/src/main/java/com/mathgenerator/service/UserService.java index f154390..7a6375b 100644 --- a/src/main/java/com/mathgenerator/service/UserService.java +++ b/src/main/java/com/mathgenerator/service/UserService.java @@ -17,6 +17,10 @@ import java.util.Optional; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ThreadLocalRandom; import java.util.regex.Pattern; +import org.apache.commons.mail.Email; +import org.apache.commons.mail.EmailException; +import org.apache.commons.mail.SimpleEmail; + public class UserService { private static final Path USER_FILE_PATH = Paths.get("users.json"); @@ -63,18 +67,39 @@ public class UserService { } /** - * (模拟) 发送验证码。 + * (已更新) 发送真实的邮件验证码。 * @param email 用户的邮箱 - * @return 生成的6位验证码 + * @return 成功发送则返回生成的6位验证码, 失败则返回null */ public String sendVerificationCode(String email) { String code = String.format("%06d", ThreadLocalRandom.current().nextInt(100000, 1000000)); - // 在真实项目中,这里会调用邮件API。我们在此处模拟。 - System.out.println("====== 验证码模拟发送 ======"); - System.out.println("发往邮箱: " + email); - System.out.println("验证码: " + code); - System.out.println("============================"); - return code; + + try { + Email mail = new SimpleEmail(); + + // 1. 设置SMTP服务器信息 + mail.setHostName(EmailConfig.getHost()); + mail.setSmtpPort(EmailConfig.getPort()); + mail.setAuthentication(EmailConfig.getUsername(), EmailConfig.getPassword()); + mail.setSSLOnConnect(true); // 开启SSL加密 + + // 2. 设置邮件内容 + mail.setFrom(EmailConfig.getUsername()); // 发件人 + mail.setSubject("【数学学习软件】您的注册验证码"); // 邮件主题 + mail.setMsg("您好!\n\n感谢您注册数学学习软件。您的验证码是:" + code + "\n\n请在5分钟内使用。"); // 邮件正文 + mail.addTo(email); // 收件人 + + // 3. 发送邮件 + mail.send(); + + System.out.println("验证码邮件已成功发送至: " + email); + return code; + + } catch (EmailException e) { + System.err.println("错误:发送验证码邮件失败!请检查您的 config.properties 配置或网络连接。"); + e.printStackTrace(); + return null; // 发送失败 + } } /** -- 2.34.1 From 76dd3a3f6f4e83dc356ef8ccfd8d61cf79950c40 Mon Sep 17 00:00:00 2001 From: smallbailangui Date: Tue, 7 Oct 2025 21:09:33 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BE=9D=E8=B5=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pom.xml b/pom.xml index 8fdb633..800a271 100644 --- a/pom.xml +++ b/pom.xml @@ -46,6 +46,12 @@ javafx-graphics ${javafx.version} + + + org.apache.commons + commons-email + 1.5 + -- 2.34.1