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"));