[DAT] 发送邮件实现方式修改

This commit is contained in:
liu.zixi
2025-10-21 11:20:10 +08:00
parent bcfdf65140
commit c23c9ddf7c
4 changed files with 124 additions and 2 deletions

View File

@@ -171,6 +171,11 @@
<artifactId>javax.mail-api</artifactId>
<version>1.5.6</version>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.5.6</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>

View File

@@ -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;
}

View File

@@ -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);
}
}
}

View File

@@ -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);
}