|
|
package com.pair.util;
|
|
|
|
|
|
import javax.mail.*;
|
|
|
import javax.mail.internet.InternetAddress;
|
|
|
import javax.mail.internet.MimeMessage;
|
|
|
import java.util.Properties;
|
|
|
import java.util.regex.Matcher;
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
/**
|
|
|
* 邮件工具类
|
|
|
* 用于发送注册码邮件(可选功能)
|
|
|
* 注意:当前项目需求中,注册码是直接显示在界面上的,
|
|
|
* 不需要发送邮件,所以这个类暂时保留为空实现。
|
|
|
* 如果将来需要发送邮件,可以使用JavaMail库实现。
|
|
|
*/
|
|
|
public class EmailUtil {
|
|
|
|
|
|
|
|
|
// [用户名]@[域名主体].[顶级域名]
|
|
|
public static final Pattern EMAIL_PATTERN = Pattern.compile("^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$");
|
|
|
|
|
|
//验证邮箱格式
|
|
|
public static boolean validateEmail(String email) {
|
|
|
if (email == null || email.trim().isEmpty()) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
Matcher matcher = EMAIL_PATTERN.matcher(email);
|
|
|
return matcher.matches();
|
|
|
}
|
|
|
|
|
|
|
|
|
// ==================== 邮箱配置 ====================
|
|
|
private static final String SENDER_EMAIL = "2936213174@qq.com"; // 我的QQ邮箱
|
|
|
private static final String SENDER_PASSWORD = "gxfjdzqviasuddci"; // QQ邮箱授权码
|
|
|
private static final String SMTP_HOST = "smtp.qq.com"; // QQ邮箱SMTP服务器
|
|
|
private static final String SMTP_PORT = "587"; // 端口
|
|
|
|
|
|
/**
|
|
|
* 发送注册码到指定邮箱
|
|
|
* @param toEmail 收件人邮箱
|
|
|
* @param registrationCode 注册码
|
|
|
* @return true表示发送成功,false表示发送失败
|
|
|
*/
|
|
|
public static boolean sendRegistrationCode(String toEmail, String registrationCode) {
|
|
|
try {
|
|
|
// 1. 配置邮件服务器
|
|
|
Properties props = new Properties();
|
|
|
props.put("mail.smtp.auth", "true");
|
|
|
props.put("mail.smtp.starttls.enable", "true");
|
|
|
props.put("mail.smtp.host", SMTP_HOST);
|
|
|
props.put("mail.smtp.port", SMTP_PORT);
|
|
|
|
|
|
// 2. 创建会话
|
|
|
Session session = Session.getInstance(props, new Authenticator() {
|
|
|
@Override
|
|
|
protected PasswordAuthentication getPasswordAuthentication() {
|
|
|
return new PasswordAuthentication(SENDER_EMAIL, SENDER_PASSWORD);
|
|
|
}
|
|
|
});
|
|
|
|
|
|
// 3. 创建邮件
|
|
|
Message message = new MimeMessage(session);
|
|
|
message.setFrom(new InternetAddress(SENDER_EMAIL));
|
|
|
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail));
|
|
|
message.setSubject("注册验证码");
|
|
|
|
|
|
// 邮件内容
|
|
|
String content = "您的注册验证码是:" + registrationCode + "\n" +
|
|
|
"验证码有效期为10分钟,请尽快使用。\n" +
|
|
|
"如非本人操作,请忽略此邮件。";
|
|
|
message.setText(content);
|
|
|
|
|
|
// 4. 发送邮件
|
|
|
Transport.send(message);
|
|
|
|
|
|
System.out.println("✓ 验证码已发送到:" + toEmail);
|
|
|
return true;
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
System.err.println("✗ 邮件发送失败:" + e.getMessage());
|
|
|
e.printStackTrace();
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 发送密码重置邮件(预留接口)
|
|
|
*
|
|
|
* @param toEmail 收件人邮箱
|
|
|
* @param newPassword 新密码
|
|
|
* @return true表示发送成功
|
|
|
*/
|
|
|
public static boolean sendPasswordReset(String toEmail, String newPassword) {
|
|
|
// TODO: 将来如果需要"找回密码"功能,可以在这里实现
|
|
|
|
|
|
System.out.println("【模拟】发送密码重置邮件");
|
|
|
System.out.println("收件人: " + toEmail);
|
|
|
System.out.println("新密码: " + newPassword);
|
|
|
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 验证邮箱格式
|
|
|
* @param email 邮箱地址
|
|
|
* @return true表示格式正确
|
|
|
*/
|
|
|
// 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);
|
|
|
// }
|
|
|
//}
|
|
|
|