From 4c8e228fa2694ada2375f35b7f9ccc4d527b6536 Mon Sep 17 00:00:00 2001 From: "liu.zixi" Date: Tue, 21 Oct 2025 11:20:10 +0800 Subject: [PATCH] =?UTF-8?q?[DAT]=20=E5=8F=91=E9=80=81=E9=82=AE=E4=BB=B6?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E6=96=B9=E5=BC=8F=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- servers/boe-server-all/pom.xml | 5 + .../assistance/service/ISmtpEmailService.java | 17 ++++ .../service/impl/SmtpEmailServiceImpl.java | 94 +++++++++++++++++++ .../impl/CaseKnowledgeServiceImpl.java | 10 +- 4 files changed, 124 insertions(+), 2 deletions(-) create mode 100644 servers/boe-server-all/src/main/java/com/xboe/module/assistance/service/ISmtpEmailService.java create mode 100644 servers/boe-server-all/src/main/java/com/xboe/module/assistance/service/impl/SmtpEmailServiceImpl.java diff --git a/servers/boe-server-all/pom.xml b/servers/boe-server-all/pom.xml index 2c185edb..214bdaff 100644 --- a/servers/boe-server-all/pom.xml +++ b/servers/boe-server-all/pom.xml @@ -171,6 +171,11 @@ javax.mail-api 1.5.6 + + com.sun.mail + javax.mail + 1.5.6 + org.apache.commons diff --git a/servers/boe-server-all/src/main/java/com/xboe/module/assistance/service/ISmtpEmailService.java b/servers/boe-server-all/src/main/java/com/xboe/module/assistance/service/ISmtpEmailService.java new file mode 100644 index 00000000..91ad7f4b --- /dev/null +++ b/servers/boe-server-all/src/main/java/com/xboe/module/assistance/service/ISmtpEmailService.java @@ -0,0 +1,17 @@ +package com.xboe.module.assistance.service; + +/** + * SMTP邮件服务接口 + */ +public interface ISmtpEmailService extends IEmailService { + + /** + * 使用SMTP直接发送邮件 + * @param to 收件人邮箱 + * @param subject 邮件主题 + * @param htmlMsg 邮件内容(HTML格式) + * @param from 发件人邮箱 + * @throws Exception 发送异常 + */ + void sendMailBySmtp(String to, String subject, String htmlMsg, String from) throws Exception; +} \ No newline at end of file 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 new file mode 100644 index 00000000..1f9f2cac --- /dev/null +++ b/servers/boe-server-all/src/main/java/com/xboe/module/assistance/service/impl/SmtpEmailServiceImpl.java @@ -0,0 +1,94 @@ +package com.xboe.module.assistance.service.impl; + +import java.util.Properties; + +import javax.mail.Authenticator; +import javax.mail.Message; +import javax.mail.MessagingException; +import javax.mail.PasswordAuthentication; +import javax.mail.Session; +import javax.mail.Transport; +import javax.mail.internet.InternetAddress; +import javax.mail.internet.MimeMessage; + +import org.apache.commons.lang3.StringUtils; +import org.springframework.stereotype.Service; + +import com.xboe.module.assistance.service.ISmtpEmailService; + +@Service +public class SmtpEmailServiceImpl implements ISmtpEmailService { + + // SMTP服务器配置信息 + private static final String SMTP_HOST = "mail.boe.com.cn"; + private static final String SMTP_USERNAME = "boeu_learning"; + private static final String SMTP_PASSWORD = "boeLms20250814Syse"; + private static final String SMTP_PORT = "465"; + private static final String SMTP_ENCRYPTION = "ssl"; + + @Override + public void sendMail(String to, String subject, String htmlMsg, String from) throws Exception { + sendMailBySmtp(to, subject, htmlMsg, from); + } + + @Override + public void sendMailBySmtp(String to, String subject, String htmlMsg, String from) throws Exception { + // 检查参数 + if (StringUtils.isBlank(to)) { + throw new Exception("发送邮件失败,未指定收件人"); + } + + if (StringUtils.isBlank(subject)) { + throw new Exception("发送邮件失败,未指定邮件主题"); + } + + if (StringUtils.isBlank(htmlMsg)) { + throw new Exception("发送邮件失败,未指定邮件内容"); + } + + // 设置SMTP属性 + Properties props = new Properties(); + props.put("mail.smtp.host", SMTP_HOST); + props.put("mail.smtp.port", SMTP_PORT); + props.put("mail.smtp.auth", "true"); + + if ("ssl".equalsIgnoreCase(SMTP_ENCRYPTION)) { + props.put("mail.smtp.ssl.enable", "true"); + props.put("mail.smtp.ssl.protocols", "TLSv1.2"); + } else if ("tls".equalsIgnoreCase(SMTP_ENCRYPTION)) { + props.put("mail.smtp.starttls.enable", "true"); + } + + // 创建会话 + Session session = Session.getInstance(props, new Authenticator() { + @Override + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication(SMTP_USERNAME, SMTP_PASSWORD); + } + }); + + try { + // 创建邮件消息 + Message message = new MimeMessage(session); + + // 设置发件人 + String fromAddress = StringUtils.isNotBlank(from) ? from : SMTP_USERNAME + "@boe.com.cn"; + message.setFrom(new InternetAddress(fromAddress)); + + // 设置收件人 + message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); + + // 设置邮件主题 + message.setSubject(subject); + + // 设置邮件内容 + message.setContent(htmlMsg, "text/html;charset=UTF-8"); + + // 发送邮件 + Transport.send(message); + + } catch (MessagingException e) { + throw new Exception("发送邮件失败", e); + } + } +} \ No newline at end of file diff --git a/servers/boe-server-all/src/main/java/com/xboe/module/boecase/service/impl/CaseKnowledgeServiceImpl.java b/servers/boe-server-all/src/main/java/com/xboe/module/boecase/service/impl/CaseKnowledgeServiceImpl.java index 06f46580..6b2db692 100644 --- a/servers/boe-server-all/src/main/java/com/xboe/module/boecase/service/impl/CaseKnowledgeServiceImpl.java +++ b/servers/boe-server-all/src/main/java/com/xboe/module/boecase/service/impl/CaseKnowledgeServiceImpl.java @@ -15,6 +15,7 @@ import com.xboe.enums.CaseDocumentLogOptStatusEnum; import com.xboe.enums.CaseDocumentLogOptTypeEnum; import com.xboe.enums.CaseDocumentLogRunStatusEnum; import com.xboe.module.assistance.service.IEmailService; +import com.xboe.module.assistance.service.ISmtpEmailService; import com.xboe.module.boecase.dao.CaseDocumentLogDao; import com.xboe.module.boecase.dao.CasesDao; import com.xboe.module.boecase.entity.CaseDocumentLog; @@ -79,6 +80,9 @@ public class CaseKnowledgeServiceImpl implements ICaseKnowledgeService { @Autowired private IEmailService emailService; + @Autowired + private ISmtpEmailService smtpEmailService; + private static final String ACCESS_TOKEN_CACHE_KEY = "case:ai:access_token"; @Override @@ -1274,7 +1278,8 @@ public class CaseKnowledgeServiceImpl implements ICaseKnowledgeService { if (recipients != null && !recipients.isEmpty()) { try { String to = String.join(",", recipients); - emailService.sendMail(to, subject, content.toString(), null); +// emailService.sendMail(to, subject, content.toString(), null); + smtpEmailService.sendMail(to, subject, content.toString(), null); } catch (Exception e) { log.error("发送接口调用失败告警邮件失败", e); } @@ -1296,7 +1301,8 @@ public class CaseKnowledgeServiceImpl implements ICaseKnowledgeService { if (recipients != null && !recipients.isEmpty()) { try { String to = String.join(",", recipients); - emailService.sendMail(to, subject, content.toString(), null); +// emailService.sendMail(to, subject, content.toString(), null); + smtpEmailService.sendMail(to, subject, content.toString(), null); } catch (Exception e) { log.error("发送业务处理失败告警邮件失败", e); }