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/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
+
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; // 发送失败
+ }
}
/**