增加短信服务 #5

Merged
hnu202326010318 merged 2 commits from liguolin_branch into develop 3 months ago

3
.gitignore vendored

@ -7,4 +7,5 @@ generated_papers/
target/
users.json
legal/
lib/
lib/
config.properties

@ -46,6 +46,12 @@
<artifactId>javafx-graphics</artifactId>
<version>${javafx.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-email</artifactId>
<version>1.5</version>
</dependency>
</dependencies>
<build>

@ -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

@ -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");
}
}

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

Loading…
Cancel
Save