|
|
package Send_Email;
|
|
|
|
|
|
import Base.Email_settings;
|
|
|
|
|
|
import javax.mail.*;
|
|
|
import javax.mail.internet.InternetAddress;
|
|
|
import javax.mail.internet.MimeMessage;
|
|
|
import java.util.Properties;
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
public class Send_email {
|
|
|
public static String judge_email_address(String email) {
|
|
|
if (email == null || email.trim().isEmpty()) {
|
|
|
return "邮箱不能为空";
|
|
|
}
|
|
|
// 去除前后空格
|
|
|
email = email.trim();
|
|
|
// 检查长度
|
|
|
if (email.length() > 254) { // RFC标准规定邮箱最大长度
|
|
|
return "邮箱地址过长";
|
|
|
}
|
|
|
// 检查是否包含@
|
|
|
if (!email.contains("@")) {
|
|
|
return "邮箱格式错误:缺少@符号";
|
|
|
}
|
|
|
// 分割本地部分和域名部分
|
|
|
String[] parts = email.split("@");
|
|
|
if (parts.length != 2) {
|
|
|
return "邮箱格式错误:只能有一个@符号";
|
|
|
}
|
|
|
String localPart = parts[0];
|
|
|
String domainPart = parts[1];
|
|
|
// 验证本地部分
|
|
|
if (localPart.isEmpty()) {
|
|
|
return "邮箱格式错误:@前必须有内容";
|
|
|
}
|
|
|
if (localPart.length() > 64) {
|
|
|
return "邮箱格式错误:@前内容过长";
|
|
|
}
|
|
|
// 验证域名部分
|
|
|
if (domainPart.isEmpty()) {
|
|
|
return "邮箱格式错误:@后必须有内容";
|
|
|
}
|
|
|
if (!domainPart.contains(".")) {
|
|
|
return "邮箱格式错误:域名不完整";
|
|
|
}
|
|
|
// 验证顶级域名
|
|
|
String[] domainParts = domainPart.split("\\.");
|
|
|
String topLevelDomain = domainParts[domainParts.length - 1];
|
|
|
if (topLevelDomain.length() < 2) {
|
|
|
return "邮箱格式错误:顶级域名太短";
|
|
|
}
|
|
|
String emailRegex = "^(?=.{1,64}@)[A-Za-z0-9_-]+(\\.[A-Za-z0-9_-]+)*@"
|
|
|
+ "[^-][A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*(\\.[A-Za-z]{2,})$";
|
|
|
boolean matches = Pattern.matches(emailRegex, email);
|
|
|
return matches ? "邮箱格式正确" : "邮箱格式错误";
|
|
|
}
|
|
|
|
|
|
public static boolean send_email(String toEmail, String Code) {
|
|
|
try {
|
|
|
Properties props = new Properties();
|
|
|
props.put("mail.smtp.host", Email_settings.SMTP_HOST);
|
|
|
props.put("mail.smtp.port", Email_settings.SMTP_PORT);
|
|
|
props.put("mail.smtp.auth", "true");
|
|
|
if (Email_settings.SSL_ENABLE) {
|
|
|
props.put("mail.smtp.starttls.enable", "true");
|
|
|
props.put("mail.smtp.ssl.trust", Email_settings.SMTP_HOST);
|
|
|
}
|
|
|
// 创建会话
|
|
|
Session session = Session.getInstance(props, new Authenticator() {
|
|
|
@Override
|
|
|
protected PasswordAuthentication getPasswordAuthentication() {
|
|
|
return new PasswordAuthentication(Email_settings.FROM_EMAIL, Email_settings.EMAIL_PASSWORD);
|
|
|
}
|
|
|
});
|
|
|
// 创建邮件消息
|
|
|
Message message = new MimeMessage(session);
|
|
|
message.setFrom(new InternetAddress(Email_settings.FROM_EMAIL));
|
|
|
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail));
|
|
|
message.setSubject("注册验证码");
|
|
|
// 邮件内容
|
|
|
String content = String.format(
|
|
|
"尊敬的用户:<b>%s</b><br/><br/>" +
|
|
|
"您的注册验证码是:<b>%s</b><br/><br/>" +
|
|
|
"验证码有效期为5分钟,请尽快完成注册。<br/><br/>" +
|
|
|
"如果不是您本人操作,请忽略此邮件。",
|
|
|
toEmail,Code
|
|
|
);
|
|
|
message.setContent(content, "text/html;charset=UTF-8");
|
|
|
// 发送邮件
|
|
|
Transport.send(message);
|
|
|
return true;
|
|
|
} catch (Exception e) {
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
}
|