From 03c5ea22d1dd56cad07b5c9b99045e0d826febbd Mon Sep 17 00:00:00 2001 From: CaptainB Date: Mon, 15 May 2023 17:58:03 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E9=A6=96=E6=AC=A1?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=82=AE=E7=AE=B1=E4=BF=A1=E6=81=AF=E6=9C=AA?= =?UTF-8?q?=E4=BF=9D=E5=AD=98=E4=B8=8D=E8=83=BD=E6=89=A7=E8=A1=8C=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E8=BF=9E=E6=8E=A5=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/SystemParameterService.java | 47 +++++++++++++++++-- 1 file changed, 42 insertions(+), 5 deletions(-) diff --git a/framework/sdk-parent/sdk/src/main/java/io/metersphere/service/SystemParameterService.java b/framework/sdk-parent/sdk/src/main/java/io/metersphere/service/SystemParameterService.java index 7da1af15f0..684a172f33 100644 --- a/framework/sdk-parent/sdk/src/main/java/io/metersphere/service/SystemParameterService.java +++ b/framework/sdk-parent/sdk/src/main/java/io/metersphere/service/SystemParameterService.java @@ -32,6 +32,8 @@ import io.metersphere.request.HeaderRequest; import io.metersphere.request.resourcepool.QueryResourcePoolRequest; import jakarta.annotation.Resource; import jakarta.mail.MessagingException; +import jakarta.mail.internet.InternetAddress; +import jakarta.mail.internet.MimeMessage; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.apache.ibatis.session.ExecutorType; @@ -39,6 +41,7 @@ import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionUtils; import org.springframework.mail.javamail.JavaMailSenderImpl; +import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -135,12 +138,46 @@ public class SystemParameterService { String recipients = hashMap.get(ParamConstants.MAIL.RECIPIENTS.getValue()); if (!StringUtils.isBlank(recipients)) { - NoticeModel noticeModel = NoticeModel.builder() - .subject("MeterSphere测试邮件") - .receivers(Arrays.asList(new Receiver(recipients, recipients))) - .build(); try { - mailNoticeSender.sendExternalMail("这是一封测试邮件,邮件发送成功", noticeModel); + MimeMessage mimeMessage = javaMailSender.createMimeMessage(); + MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); + String username = javaMailSender.getUsername(); + String email; + if (username.contains("@")) { + email = username; + } else { + String mailHost = javaMailSender.getHost(); + String domainName = mailHost.substring(mailHost.indexOf(".") + 1); + email = username + "@" + domainName; + } + InternetAddress from = new InternetAddress(); + String smtpFrom = hashMap.get(ParamConstants.MAIL.FROM.getValue()); + if (StringUtils.isBlank(smtpFrom)) { + from.setAddress(email); + from.setPersonal(username); + } else { + // 指定发件人后,address 应该是邮件服务器验证过的发件人 + if (smtpFrom.contains("@")) { + from.setAddress(smtpFrom); + } else { + from.setAddress(email); + } + from.setPersonal(smtpFrom); + } + helper.setFrom(from); + + LogUtil.debug("发件人地址" + javaMailSender.getUsername()); + LogUtil.debug("helper" + helper); + helper.setSubject("MeterSphere测试邮件"); + + LogUtil.info("收件人地址: {}", Arrays.asList(recipients)); + helper.setText("这是一封测试邮件,邮件发送成功", true); + helper.setTo(recipients); + try { + javaMailSender.send(mimeMessage); + } catch (Exception e) { + LogUtil.error("发送邮件失败: ", e); + } } catch (Exception e) { LogUtil.error(e); MSException.throwException(Translator.get("connection_failed"));