fix:邮件配置中SMTP账号是如果用户名,而不是一个邮件地址的情况

This commit is contained in:
wenyann 2021-07-08 13:57:37 +08:00 committed by 刘瑞斌
parent b68680bddc
commit da9b5607b5
2 changed files with 19 additions and 4 deletions

View File

@ -24,9 +24,16 @@ public class MailNoticeSender extends AbstractNoticeSender {
JavaMailSenderImpl javaMailSender = mailService.getMailSender();
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
helper.setFrom(javaMailSender.getUsername());
LogUtil.info("发件人地址"+javaMailSender.getUsername());
LogUtil.info("helper"+helper);
//helper.setFrom(javaMailSender.getUsername());
if (javaMailSender.getUsername().contains("@")) {
helper.setFrom(javaMailSender.getUsername());
} else {
String mailHost = javaMailSender.getHost();
String domainName = mailHost.substring(mailHost.indexOf(".") + 1, mailHost.length());
helper.setFrom(javaMailSender.getUsername() + "@" + domainName);
}
LogUtil.info("发件人地址" + javaMailSender.getUsername());
LogUtil.info("helper" + helper);
helper.setSubject("MeterSphere " + noticeModel.getSubject());
List<String> emails = super.getUserEmails(messageDetail.getUserIds());
String[] users = emails.toArray(new String[0]);

View File

@ -112,6 +112,8 @@ public class SystemParameterService {
}
if (BooleanUtils.toBoolean(hashMap.get(ParamConstants.MAIL.TLS.getValue()))) {
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.ssl.trust", hashMap.get(ParamConstants.MAIL.SERVER.getValue()));
}
props.put("mail.smtp.timeout", "30000");
props.put("mail.smtp.connectiontimeout", "5000");
@ -127,7 +129,13 @@ public class SystemParameterService {
MimeMessageHelper helper = null;
try {
helper = new MimeMessageHelper(mimeMessage, true);
helper.setFrom(javaMailSender.getUsername());
if (javaMailSender.getUsername().contains("@")) {
helper.setFrom(javaMailSender.getUsername());
} else {
String mailHost = javaMailSender.getHost();
String domainName = mailHost.substring(mailHost.indexOf(".") + 1, mailHost.length());
helper.setFrom(javaMailSender.getUsername() + "@" + domainName);
}
helper.setSubject("MeterSphere测试邮件 ");
helper.setText("这是一封测试邮件,邮件发送成功", true);
helper.setTo(recipients);