邮件配置不取字典

This commit is contained in:
liu.zixi
2025-11-17 17:19:28 +08:00
parent dfba210890
commit 09c10e3af6

View File

@@ -13,11 +13,8 @@ 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;
@@ -34,9 +31,6 @@ public class SmtpEmailServiceImpl implements ISmtpEmailService {
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 {
// 检查参数
@@ -52,60 +46,25 @@ public class SmtpEmailServiceImpl implements ISmtpEmailService {
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", smtpHost);
props.put("mail.smtp.port", smtpPort);
props.put("mail.smtp.host", SMTP_HOST);
props.put("mail.smtp.port", SMTP_PORT);
props.put("mail.smtp.auth", "true");
if ("ssl".equalsIgnoreCase(smtpEncryption)) {
if ("ssl".equalsIgnoreCase(SMTP_ENCRYPTION)) {
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.ssl.trust", smtpHost);
props.put("mail.smtp.ssl.trust", SMTP_HOST);
// props.put("mail.smtp.ssl.protocols", "TLSv1.2");
} else if ("tls".equalsIgnoreCase(smtpEncryption)) {
} else if ("tls".equalsIgnoreCase(SMTP_ENCRYPTION)) {
props.put("mail.smtp.starttls.enable", "true");
}
Authenticator authenticator = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(smtpUsername, smtpPassword);
return new PasswordAuthentication(SMTP_USERNAME, SMTP_PASSWORD);
}
};
// 创建会话
@@ -117,7 +76,7 @@ public class SmtpEmailServiceImpl implements ISmtpEmailService {
Message message = new MimeMessage(session);
// 设置发件人
message.setFrom(new InternetAddress(smtpUsername));
message.setFrom(new InternetAddress(SMTP_USERNAME));
// 设置收件人
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
@@ -132,7 +91,7 @@ public class SmtpEmailServiceImpl implements ISmtpEmailService {
message.setSentDate(new Date());
// 发送邮件
log.info("发送邮件. 发件人: {}, 收件人: {}, 标题: {}", smtpUsername, to, subject);
log.info("发送邮件. 发件人: {}, 收件人: {}, 标题: {}", SMTP_USERNAME, to, subject);
Transport.send(message);
} catch (MessagingException e) {