mirror of
https://codeup.aliyun.com/67762337eccfc218f6110e0e/per-boe/java-servers.git
synced 2025-12-09 02:46:50 +08:00
邮件配置更新,并改为可获取字典
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package com.xboe.module.assistance.service.impl;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.mail.Authenticator;
|
||||
@@ -12,8 +13,11 @@ import javax.mail.Transport;
|
||||
import javax.mail.internet.InternetAddress;
|
||||
import javax.mail.internet.MimeMessage;
|
||||
|
||||
import com.xboe.module.dict.entity.DictItem;
|
||||
import com.xboe.module.dict.service.ISysDictionaryService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.xboe.module.assistance.service.ISmtpEmailService;
|
||||
@@ -22,12 +26,16 @@ import com.xboe.module.assistance.service.ISmtpEmailService;
|
||||
@Slf4j
|
||||
public class SmtpEmailServiceImpl implements ISmtpEmailService {
|
||||
|
||||
// SMTP服务器配置信息
|
||||
//region 默认SMTP服务器配置信息
|
||||
private static final String SMTP_HOST = "mail.boe.com.cn";
|
||||
private static final String SMTP_USERNAME = "boeu_learning@boe.com.cn";
|
||||
private static final String SMTP_PASSWORD = "boeLms20250814Syse";
|
||||
private static final String SMTP_PASSWORD = "boeLms20251112Syse";
|
||||
private static final String SMTP_PORT = "465";
|
||||
private static final String SMTP_ENCRYPTION = "ssl";
|
||||
//endregion
|
||||
|
||||
@Autowired
|
||||
private ISysDictionaryService sysDictionaryService;
|
||||
|
||||
@Override
|
||||
public void sendMailBySmtp(String to, String subject, String htmlMsg, String from) throws Exception {
|
||||
@@ -43,25 +51,61 @@ public class SmtpEmailServiceImpl implements ISmtpEmailService {
|
||||
if (StringUtils.isBlank(htmlMsg)) {
|
||||
throw new Exception("发送邮件失败,未指定邮件内容");
|
||||
}
|
||||
// 初始化配置项
|
||||
String smtpHost;
|
||||
String smtpPort;
|
||||
String smtpEncryption;
|
||||
String smtpUsername;
|
||||
String smtpPassword;
|
||||
DictItem smtpHostItem = sysDictionaryService.detail("case_ai_alert_mail", "smtp_host");
|
||||
if (smtpHostItem != null) {
|
||||
smtpHost = smtpHostItem.getName();
|
||||
} else {
|
||||
smtpHost = SMTP_HOST;
|
||||
}
|
||||
DictItem smtpPortItem = sysDictionaryService.detail("case_ai_alert_mail", "smtp_port");
|
||||
if (smtpPortItem != null) {
|
||||
smtpPort = smtpPortItem.getName();
|
||||
} else {
|
||||
smtpPort = SMTP_PORT;
|
||||
}
|
||||
DictItem smtpEncryptionItem = sysDictionaryService.detail("case_ai_alert_mail", "smtp_encryption");
|
||||
if (smtpEncryptionItem != null) {
|
||||
smtpEncryption = smtpEncryptionItem.getName();
|
||||
} else {
|
||||
smtpEncryption = SMTP_ENCRYPTION;
|
||||
}
|
||||
DictItem smtpUsernameItem = sysDictionaryService.detail("case_ai_alert_mail", "smtp_username");
|
||||
if (smtpUsernameItem != null) {
|
||||
smtpUsername = smtpUsernameItem.getName();
|
||||
} else {
|
||||
smtpUsername = SMTP_USERNAME;
|
||||
}
|
||||
DictItem smtpPasswordItem = sysDictionaryService.detail("case_ai_alert_mail", "smtp_password");
|
||||
if (smtpPasswordItem != null) {
|
||||
smtpPassword = smtpPasswordItem.getName();
|
||||
} else {
|
||||
smtpPassword = SMTP_PASSWORD;
|
||||
}
|
||||
|
||||
// 设置SMTP属性
|
||||
Properties props = new Properties();
|
||||
props.put("mail.smtp.host", SMTP_HOST);
|
||||
props.put("mail.smtp.port", SMTP_PORT);
|
||||
props.put("mail.smtp.host", smtpHost);
|
||||
props.put("mail.smtp.port", smtpPort);
|
||||
props.put("mail.smtp.auth", "true");
|
||||
|
||||
if ("ssl".equalsIgnoreCase(SMTP_ENCRYPTION)) {
|
||||
if ("ssl".equalsIgnoreCase(smtpEncryption)) {
|
||||
props.put("mail.smtp.ssl.enable", "true");
|
||||
props.put("mail.smtp.ssl.trust", SMTP_HOST);
|
||||
props.put("mail.smtp.ssl.trust", smtpHost);
|
||||
// props.put("mail.smtp.ssl.protocols", "TLSv1.2");
|
||||
} else if ("tls".equalsIgnoreCase(SMTP_ENCRYPTION)) {
|
||||
} else if ("tls".equalsIgnoreCase(smtpEncryption)) {
|
||||
props.put("mail.smtp.starttls.enable", "true");
|
||||
}
|
||||
|
||||
Authenticator authenticator = new Authenticator() {
|
||||
@Override
|
||||
protected PasswordAuthentication getPasswordAuthentication() {
|
||||
return new PasswordAuthentication(SMTP_USERNAME, SMTP_PASSWORD);
|
||||
return new PasswordAuthentication(smtpUsername, smtpPassword);
|
||||
}
|
||||
};
|
||||
// 创建会话
|
||||
@@ -73,7 +117,7 @@ public class SmtpEmailServiceImpl implements ISmtpEmailService {
|
||||
Message message = new MimeMessage(session);
|
||||
|
||||
// 设置发件人
|
||||
message.setFrom(new InternetAddress(SMTP_USERNAME));
|
||||
message.setFrom(new InternetAddress(smtpUsername));
|
||||
|
||||
// 设置收件人
|
||||
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
|
||||
@@ -88,7 +132,7 @@ public class SmtpEmailServiceImpl implements ISmtpEmailService {
|
||||
message.setSentDate(new Date());
|
||||
|
||||
// 发送邮件
|
||||
log.info("发送邮件. 发件人: {}, 收件人: {}, 标题: {}", SMTP_USERNAME, to, subject);
|
||||
log.info("发送邮件. 发件人: {}, 收件人: {}, 标题: {}", smtpUsername, to, subject);
|
||||
Transport.send(message);
|
||||
|
||||
} catch (MessagingException e) {
|
||||
|
||||
Reference in New Issue
Block a user