You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
wang2/src/com/mathlearning/util/EmailUtil.java

116 lines
4.4 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.mathlearning.util;
import javax.mail.*;
import javax.mail.internet.*;
import javax.swing.JOptionPane;
import java.util.Properties;
import java.util.Random;
public class EmailUtil {
private static final Random random = new Random();
public static String sendVerificationCode(String toEmail) {
if (!EmailConfig.isConfigured()) {
JOptionPane.showMessageDialog(null,
"邮箱配置未完成!\n请修改 email_config.properties 文件,配置您的邮箱信息。",
"配置错误",
JOptionPane.ERROR_MESSAGE);
return null;
}
String verificationCode = generateVerificationCode();
try {
boolean sendSuccess = sendEmail(toEmail, verificationCode);
if (sendSuccess) {
JOptionPane.showMessageDialog(
null,
"验证码已发送到您的邮箱!\n\n" +
"收件邮箱: " + toEmail + "\n" +
"请查看您的邮箱并输入验证码。",
"验证码发送成功",
JOptionPane.INFORMATION_MESSAGE
);
return verificationCode;
} else {
JOptionPane.showMessageDialog(null,
"验证码发送失败,请检查邮箱地址是否正确或稍后重试",
"发送失败",
JOptionPane.ERROR_MESSAGE);
return null;
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null,
"发送验证码时发生错误: " + e.getMessage(),
"错误",
JOptionPane.ERROR_MESSAGE);
return null;
}
}
private static boolean sendEmail(String toEmail, String verificationCode) {
Properties props = new Properties();
props.put("mail.smtp.host", EmailConfig.getSmtpHost());
props.put("mail.smtp.port", EmailConfig.getSmtpPort());
props.put("mail.smtp.auth", "true");
if (EmailConfig.isSslEnabled()) {
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.port", EmailConfig.getSmtpPort());
}
Session session = Session.getInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(EmailConfig.getFrom(), EmailConfig.getPassword());
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(EmailConfig.getFrom()));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail));
message.setSubject("数学学习软件 - 邮箱验证码");
String emailContent = "尊敬的用户:\n\n" +
"您正在注册数学学习软件,验证码为:" + verificationCode + "\n\n" +
"验证码有效期为10分钟请尽快完成注册。\n\n" +
"如果不是您本人操作,请忽略此邮件。\n\n" +
"数学学习软件团队";
message.setText(emailContent);
Transport.send(message);
System.out.println("验证码邮件已发送到: " + toEmail);
return true;
} catch (MessagingException e) {
System.err.println("发送邮件失败: " + e.getMessage());
e.printStackTrace();
return false;
}
}
private static String generateVerificationCode() {
return String.format("%06d", random.nextInt(1000000));
}
public static boolean isValidEmail(String email) {
if (email == null || email.trim().isEmpty()) {
return false;
}
String emailRegex = "^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$";
return email.matches(emailRegex);
}
public static String getEmailRequirements() {
return "请输入有效的邮箱地址例如username@example.com";
}
}