From 64034e6b045d0135c5955e59ebba134d2feeb822 Mon Sep 17 00:00:00 2001 From: "liu.zixi" Date: Mon, 17 Nov 2025 16:34:17 +0800 Subject: [PATCH] =?UTF-8?q?=E9=82=AE=E4=BB=B6=E9=85=8D=E7=BD=AE=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=EF=BC=8C=E5=B9=B6=E6=94=B9=E4=B8=BA=E5=8F=AF=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E5=AD=97=E5=85=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/SmtpEmailServiceImpl.java | 64 ++++++++++++++++--- 1 file changed, 54 insertions(+), 10 deletions(-) diff --git a/servers/boe-server-all/src/main/java/com/xboe/module/assistance/service/impl/SmtpEmailServiceImpl.java b/servers/boe-server-all/src/main/java/com/xboe/module/assistance/service/impl/SmtpEmailServiceImpl.java index 9ec9f57a..8d8f2c67 100644 --- a/servers/boe-server-all/src/main/java/com/xboe/module/assistance/service/impl/SmtpEmailServiceImpl.java +++ b/servers/boe-server-all/src/main/java/com/xboe/module/assistance/service/impl/SmtpEmailServiceImpl.java @@ -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) {