Skip to content

Commit

Permalink
fix: 修复首次添加邮箱信息未保存不能执行测试连接的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
liuruibin committed May 15, 2023
1 parent 32f2368 commit 7682470
Showing 1 changed file with 48 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@
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;
Expand Down Expand Up @@ -113,12 +116,46 @@ public void testConnection(HashMap<String, String> hashMap) {

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"));
Expand Down Expand Up @@ -264,20 +301,20 @@ public void saveBaseInfo(List<SystemParameter> parameters) {
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);
Expand Down

0 comments on commit 7682470

Please sign in to comment.