修改bug
This commit is contained in:
@@ -27,13 +27,11 @@ public class DynamicMailConfig {
|
||||
public JavaMailSender createMailSender(OaEmailAccount account) {
|
||||
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
|
||||
|
||||
// 设置SMTP服务器
|
||||
// 判断是否为网易企业邮箱(如 freeqiye.com)
|
||||
if (account.getEmail() != null && account.getEmail().toLowerCase().endsWith("@freeqiye.com")) {
|
||||
mailSender.setHost("smtp.qiye.163.com");
|
||||
} else {
|
||||
mailSender.setHost(account.getSmtpHost());
|
||||
}
|
||||
// 设置SMTP服务器 - 根据邮箱域名自动识别企业邮箱
|
||||
String email = account.getEmail();
|
||||
String smtpHost = getSmtpHostByEmail(email, account.getSmtpHost());
|
||||
|
||||
mailSender.setHost(smtpHost);
|
||||
mailSender.setPort(account.getSmtpPort() == null ? 465 : account.getSmtpPort().intValue());
|
||||
mailSender.setUsername(account.getEmail());
|
||||
mailSender.setPassword(account.getPassword());
|
||||
@@ -86,4 +84,120 @@ public class DynamicMailConfig {
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
// 测试方法
|
||||
public static void main(String[] args) {
|
||||
DynamicMailConfig config = new DynamicMailConfig();
|
||||
|
||||
// 测试各种邮箱类型
|
||||
String[] testEmails = {
|
||||
"test@freeqiye.com", // 网易企业邮箱
|
||||
"test@qiye.163.com", // 网易企业邮箱
|
||||
"test@163.com", // 网易个人邮箱
|
||||
"test@126.com", // 网易126邮箱
|
||||
"test@qq.com", // QQ邮箱
|
||||
"test@exmail.qq.com", // QQ企业邮箱
|
||||
"test@aliyun.com", // 阿里云邮箱
|
||||
"test@sina.com", // 新浪邮箱
|
||||
"test@vip.sina.com", // 新浪VIP邮箱
|
||||
"test@sohu.com", // 搜狐邮箱
|
||||
"test@139.com", // 139邮箱
|
||||
"test@189.cn", // 189邮箱
|
||||
"test@feishu.cn", // 飞书邮箱
|
||||
"test@gmail.com", // Gmail邮箱
|
||||
"test@outlook.com", // Outlook邮箱
|
||||
"test@hotmail.com", // Hotmail邮箱
|
||||
"test@company.com" // 自定义企业邮箱
|
||||
};
|
||||
|
||||
System.out.println("邮箱SMTP服务器识别测试:");
|
||||
System.out.println("==========================================");
|
||||
|
||||
for (String email : testEmails) {
|
||||
String smtpHost = config.getSmtpHostByEmail(email, null);
|
||||
System.out.printf("%-20s -> %s%n", email, smtpHost);
|
||||
}
|
||||
|
||||
System.out.println("==========================================");
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据邮箱地址自动识别SMTP服务器
|
||||
*
|
||||
* @param email 邮箱地址
|
||||
* @param defaultSmtpHost 默认SMTP服务器
|
||||
* @return SMTP服务器地址
|
||||
*/
|
||||
public String getSmtpHostByEmail(String email, String defaultSmtpHost) {
|
||||
if (email == null) {
|
||||
return defaultSmtpHost != null ? defaultSmtpHost : "smtp.163.com";
|
||||
}
|
||||
|
||||
String domain = email.toLowerCase();
|
||||
|
||||
// 网易企业邮箱
|
||||
if (domain.contains("@freeqiye.com") || domain.contains("@qiye.163.com")) {
|
||||
return "smtp.qiye.163.com";
|
||||
}
|
||||
// 网易个人邮箱
|
||||
else if (domain.contains("@163.com")) {
|
||||
return "smtp.163.com";
|
||||
}
|
||||
// 网易126邮箱
|
||||
else if (domain.contains("@126.com")) {
|
||||
return "smtp.126.com";
|
||||
}
|
||||
// QQ邮箱
|
||||
else if (domain.contains("@qq.com")) {
|
||||
return "smtp.qq.com";
|
||||
}
|
||||
// QQ企业邮箱
|
||||
else if (domain.contains("@exmail.qq.com")) {
|
||||
return "smtp.exmail.qq.com";
|
||||
}
|
||||
// 阿里云邮箱
|
||||
else if (domain.contains("@aliyun.com")) {
|
||||
return "smtp.aliyun.com";
|
||||
}
|
||||
// 新浪邮箱
|
||||
else if (domain.contains("@sina.com")) {
|
||||
return "smtp.sina.com";
|
||||
}
|
||||
// 新浪VIP邮箱
|
||||
else if (domain.contains("@vip.sina.com")) {
|
||||
return "smtp.vip.sina.com";
|
||||
}
|
||||
// 搜狐邮箱
|
||||
else if (domain.contains("@sohu.com")) {
|
||||
return "smtp.sohu.com";
|
||||
}
|
||||
// 139邮箱
|
||||
else if (domain.contains("@139.com")) {
|
||||
return "smtp.139.com";
|
||||
}
|
||||
// 189邮箱
|
||||
else if (domain.contains("@189.cn")) {
|
||||
return "smtp.189.cn";
|
||||
}
|
||||
// 飞书邮箱
|
||||
else if (domain.contains("@feishu.cn")) {
|
||||
return "smtp.feishu.cn";
|
||||
}
|
||||
// Gmail邮箱
|
||||
else if (domain.contains("@gmail.com")) {
|
||||
return "smtp.gmail.com";
|
||||
}
|
||||
// Outlook邮箱
|
||||
else if (domain.contains("@outlook.com") || domain.contains("@hotmail.com")) {
|
||||
return "smtp-mail.outlook.com";
|
||||
}
|
||||
// 企业邮箱通用域名识别(可以根据需要添加更多)
|
||||
else if (domain.contains("@qiye.") || domain.contains("@corp.") || domain.contains("@enterprise.")) {
|
||||
// 对于企业邮箱,优先使用配置的SMTP服务器,如果没有配置则使用通用企业邮箱SMTP
|
||||
return defaultSmtpHost != null ? defaultSmtpHost : "smtp." + domain.split("@")[1];
|
||||
}
|
||||
|
||||
// 如果都不匹配,使用配置的SMTP服务器或默认服务器
|
||||
return defaultSmtpHost != null ? defaultSmtpHost : "smtp.163.com";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user