前端2.0带邮箱

lyq
LYQ 4 months ago
parent add58d0a32
commit 05dbc0b18a

@ -12,5 +12,15 @@
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" exported="" name="lib" level="application" />
<orderEntry type="library" exported="" name="l" level="application" />
<orderEntry type="module-library" exported="">
<library>
<CLASSES>
<root url="file://$MODULE_DIR$/lib" />
</CLASSES>
<JAVADOC />
<SOURCES />
<jarDirectory url="file://$MODULE_DIR$/lib" recursive="false" />
</library>
</orderEntry>
</component>
</module>

Binary file not shown.

Binary file not shown.

@ -0,0 +1,83 @@
package com.student.mathquiz.view;
import jakarta.mail.*;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeMessage;
import java.util.Properties;
import java.util.Random;
public class EmailService {
// ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
// ★ 把这里换成你自己的发件人邮箱和授权码! ★
// ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
private static final String SENDER_EMAIL = "3048494657@qq.com";
private static final String SENDER_AUTHORIZATION_CODE = "ymnbezwjzrnfdgih";
/**
* 6.
* @return 6
*/
public static String generateVerificationCode() {
Random random = new Random();
int code = 100000 + random.nextInt(900000); // 保证是6位数
return String.valueOf(code);
}
/**
* .
* @param recipientEmail
* @param code
* @return true, false
*/
public static boolean sendVerificationEmail(String recipientEmail, String code) {
// 1. 配置邮件服务器属性
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.qq.com"); // QQ邮箱的SMTP服务器地址
props.put("mail.smtp.port", "587"); // SMTP服务器端口 (使用TLS加密)
props.put("mail.smtp.auth", "true"); // 需要身份验证
props.put("mail.smtp.starttls.enable", "true"); // 启用TLS加密
// 2. 创建认证器,用于登录邮箱
Authenticator authenticator = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(SENDER_EMAIL, SENDER_AUTHORIZATION_CODE);
}
};
// 3. 获取邮件会话 Session
Session session = Session.getInstance(props, authenticator);
try {
// 4. 创建邮件消息 MimeMessage
MimeMessage message = new MimeMessage(session);
// 设置发件人
message.setFrom(new InternetAddress(SENDER_EMAIL));
// 设置收件人
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipientEmail));
// 设置邮件主题
message.setSubject("【数学学习软件】您的注册验证码");
// 设置邮件正文内容
message.setText("尊敬的用户,您好!\n\n您的注册验证码是" + code + "\n\n请在5分钟内完成注册。如果不是您本人操作请忽略此邮件。\n\n(此为系统自动发送,请勿回复)");
// 5. 发送邮件!
Transport.send(message);
System.out.println("验证码邮件发送成功!");
return true;
} catch (MessagingException e) {
e.printStackTrace();
System.out.println("邮件发送失败!");
return false;
}
}
// 你可以写一个临时的 main 方法来测试它!
/* public static void main(String[] args) {
String code = generateVerificationCode();
// ★★★ 把这里换成你自己的另一个邮箱来接收测试邮件 ★★★
sendVerificationEmail("lyqqqq1214@163.com", code);
}*/
}

@ -2,6 +2,7 @@
<!-- RegisterView.fxml -->
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.Font?>
<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.student.mathquiz.view.RegisterViewController">

@ -2,10 +2,14 @@
package com.student.mathquiz.view;
import com.student.mathquiz.MainApp;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
public class RegisterViewController {
public TextField codeField;
public TextField emailField;
@FXML private Label statusLabel;
private MainApp mainApp;
@ -17,7 +21,27 @@ public class RegisterViewController {
@FXML
private void handleSendCode() {
// TODO: 在这里调用后端 userService.sendVerificationCode(...)
statusLabel.setText("验证码已发送(伪)!");
String email = emailField.getText();
// TODO: 在这里先加一个邮箱格式校验
String code = EmailService.generateVerificationCode();
// ★★★ 在后台线程中发送邮件,防止界面卡死! ★★★
new Thread(() -> {
boolean success = EmailService.sendVerificationEmail(email, code);
// 在 JavaFX 主线程中更新界面
Platform.runLater(() -> {
if (success) {
statusLabel.setText("验证码已发送至您的邮箱,请查收!");
// TODO: 把 code 和 email 存起来,用于稍后的验证
} else {
statusLabel.setText("验证码发送失败,请检查邮箱地址或网络!");
}
});
}).start();
}
@FXML
@ -32,3 +56,4 @@ public class RegisterViewController {
mainApp.showLoginView();
}
}
// 在 RegisterViewController.java 里

Loading…
Cancel
Save