fix: 修复首次添加邮箱信息未保存不能执行测试连接的问题

This commit is contained in:
CaptainB 2023-05-15 17:52:51 +08:00 committed by 刘瑞斌
parent 32f2368c9c
commit 768247016f
1 changed files with 48 additions and 11 deletions

View File

@ -28,11 +28,14 @@ import io.metersphere.notice.sender.impl.MailNoticeSender;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
@ -113,12 +116,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"));
@ -264,20 +301,20 @@ public class SystemParameterService {
example.createCriteria().andParamKeyEqualTo(param.getParamKey());
List<SystemParameter> baseUrlParameterList = systemParameterMapper.selectByExample(example);
String oldBaseUrl = null;
if(CollectionUtils.isNotEmpty(baseUrlParameterList)){
if (CollectionUtils.isNotEmpty(baseUrlParameterList)) {
SystemParameter parameter = baseUrlParameterList.get(0);
if(!StringUtils.equals(parameter.getParamValue(),param.getParamValue())){
if (!StringUtils.equals(parameter.getParamValue(), param.getParamValue())) {
oldBaseUrl = parameter.getParamValue();
systemParameterMapper.updateByPrimaryKey(param);
}
}else {
} else {
systemParameterMapper.insert(param);
}
example.clear();
if(StringUtils.isNotEmpty(oldBaseUrl)){
apiTestEnvironmentService.batchUpdateMockEvnInfoByBaseUrl(oldBaseUrl,param.getParamValue());
if (StringUtils.isNotEmpty(oldBaseUrl)) {
apiTestEnvironmentService.batchUpdateMockEvnInfoByBaseUrl(oldBaseUrl, param.getParamValue());
}
}else {
} else {
example.createCriteria().andParamKeyEqualTo(param.getParamKey());
if (systemParameterMapper.countByExample(example) > 0) {
systemParameterMapper.updateByPrimaryKey(param);