feat: 添加邮件测试

This commit is contained in:
wenyann 2021-01-05 14:20:32 +08:00
parent ad597dcd79
commit 5aa46f6656
8 changed files with 108 additions and 74 deletions

View File

@ -88,28 +88,22 @@ public interface ParamConstants {
}
}
enum MAIL {
SERVER("smtp.server", 1),
PORT("smtp.port", 2),
ACCOUNT("smtp.account", 3),
PASSWORD("smtp.password", 4),
SSL("smtp.ssl", 5),
TLS("smtp.tls", 6),
ANON("smtp.anon", 7);
enum MAIL implements ParamConstants{
SERVER("smtp.host"),
PORT("smtp.port"),
ACCOUNT("smtp.account"),
PASSWORD("smtp.password"),
SSL("smtp.ssl"),
TLS("smtp.tls"),
RECIPIENTS("smtp.recipient");
private String key;
private Integer value;
private String value;
private MAIL(String key, Integer value) {
this.key = key;
private MAIL(String value) {
this.value = value;
}
public String getKey() {
return this.key;
}
public Integer getValue() {
public String getValue() {
return this.value;
}
}

View File

@ -5,6 +5,7 @@ import io.metersphere.commons.constants.ParamConstants;
import io.metersphere.commons.constants.RoleConstants;
import io.metersphere.dto.BaseSystemConfigDTO;
import io.metersphere.ldap.domain.LdapInfo;
import io.metersphere.notice.domain.MailInfo;
import io.metersphere.service.SystemParameterService;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;
@ -38,7 +39,7 @@ public class SystemParameterController {
@GetMapping("/mail/info")
@RequiresRoles(value = {RoleConstants.ADMIN})
public Object mailInfo() {
public MailInfo mailInfo() {
return SystemParameterService.mailInfo(ParamConstants.Classify.MAIL.getValue());
}

View File

@ -0,0 +1,15 @@
package io.metersphere.notice.domain;
import lombok.Data;
@Data
public class MailInfo {
private String host;
private String port;
private String account;
private String password;
private String ssl;
private String tls;
private String recipient;
}

View File

@ -11,15 +11,18 @@ import io.metersphere.commons.utils.LogUtil;
import io.metersphere.dto.BaseSystemConfigDTO;
import io.metersphere.i18n.Translator;
import io.metersphere.ldap.domain.LdapInfo;
import io.metersphere.notice.domain.MailInfo;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.BooleanUtils;
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.MimeMessage;
import java.util.*;
@ -56,7 +59,7 @@ public class SystemParameterService {
parameters.forEach(parameter -> {
SystemParameterExample example = new SystemParameterExample();
if (parameter.getParamKey().equals(ParamConstants.MAIL.PASSWORD.getKey())) {
if (parameter.getParamKey().equals(ParamConstants.MAIL.PASSWORD.getValue())) {
if (!StringUtils.isBlank(parameter.getParamValue())) {
String string = EncryptUtils.aesEncrypt(parameter.getParamValue()).toString();
parameter.setParamValue(string);
@ -82,23 +85,16 @@ public class SystemParameterService {
public void testConnection(HashMap<String, String> hashMap) {
JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
javaMailSender.setDefaultEncoding("UTF-8");
javaMailSender.setHost(hashMap.get(ParamConstants.MAIL.SERVER.getKey()));
javaMailSender.setPort(Integer.valueOf(hashMap.get(ParamConstants.MAIL.PORT.getKey())));
javaMailSender.setUsername(hashMap.get(ParamConstants.MAIL.ACCOUNT.getKey()));
javaMailSender.setPassword(hashMap.get(ParamConstants.MAIL.PASSWORD.getKey()));
javaMailSender.setHost(hashMap.get(ParamConstants.MAIL.SERVER.getValue()));
javaMailSender.setPort(Integer.valueOf(hashMap.get(ParamConstants.MAIL.PORT.getValue())));
javaMailSender.setUsername(hashMap.get(ParamConstants.MAIL.ACCOUNT.getValue()));
javaMailSender.setPassword(hashMap.get(ParamConstants.MAIL.PASSWORD.getValue()));
Properties props = new Properties();
boolean isAnon = Boolean.parseBoolean(hashMap.get(ParamConstants.MAIL.ANON.getKey()));
if (isAnon) {
props.put("mail.smtp.auth", "false");
javaMailSender.setUsername(null);
javaMailSender.setPassword(null);
} else {
props.put("mail.smtp.auth", "true");
}
if (BooleanUtils.toBoolean(hashMap.get(ParamConstants.MAIL.SSL.getKey()))) {
String recipients = hashMap.get(ParamConstants.MAIL.RECIPIENTS.getValue());
if (BooleanUtils.toBoolean(hashMap.get(ParamConstants.MAIL.SSL.getValue()))) {
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
}
if (BooleanUtils.toBoolean(hashMap.get(ParamConstants.MAIL.TLS.getKey()))) {
if (BooleanUtils.toBoolean(hashMap.get(ParamConstants.MAIL.TLS.getValue()))) {
props.put("mail.smtp.starttls.enable", "true");
}
props.put("mail.smtp.timeout", "30000");
@ -110,39 +106,53 @@ public class SystemParameterService {
LogUtil.error(e.getMessage(), e);
MSException.throwException(Translator.get("connection_failed"));
}
if(!StringUtils.isBlank(recipients)){
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper helper = null;
try {
helper = new MimeMessageHelper(mimeMessage, true);
helper.setFrom(javaMailSender.getUsername());
helper.setSubject("MeterSphere测试邮件 " );
helper.setText("这是一封测试邮件,邮件发送成功", true);
helper.setTo(recipients);
javaMailSender.send(mimeMessage);
} catch (MessagingException e) {
LogUtil.error(e.getMessage(), e);
MSException.throwException(Translator.get("connection_failed"));
}
}
}
public String getVersion() {
return System.getenv("MS_VERSION");
}
public Object mailInfo(String type) {
public MailInfo mailInfo(String type) {
List<SystemParameter> paramList = this.getParamList(type);
if (CollectionUtils.isEmpty(paramList)) {
paramList = new ArrayList<>();
ParamConstants.MAIL[] values = ParamConstants.MAIL.values();
for (ParamConstants.MAIL value : values) {
SystemParameter systemParameter = new SystemParameter();
if (value.equals(ParamConstants.MAIL.PASSWORD)) {
systemParameter.setType(ParamConstants.Type.PASSWORD.getValue());
} else {
systemParameter.setType(ParamConstants.Type.TEXT.getValue());
MailInfo mailInfo=new MailInfo ();
if (!CollectionUtils.isEmpty(paramList)) {
for (SystemParameter param : paramList) {
if (StringUtils.equals(param.getParamKey(),ParamConstants.MAIL.SERVER.getValue() )) {
mailInfo.setHost(param.getParamValue());
} else if (StringUtils.equals(param.getParamKey(), ParamConstants.MAIL.PORT.getValue())) {
mailInfo.setPort(param.getParamValue());
} else if (StringUtils.equals(param.getParamKey(), ParamConstants.MAIL.ACCOUNT.getValue())) {
mailInfo.setAccount(param.getParamValue());
} else if (StringUtils.equals(param.getParamKey(), ParamConstants.MAIL.PASSWORD.getValue())) {
String password = EncryptUtils.aesDecrypt(param.getParamValue()).toString();
mailInfo.setPassword(password);
} else if (StringUtils.equals(param.getParamKey(), ParamConstants.MAIL.SSL.getValue())) {
mailInfo.setSsl(param.getParamValue());
} else if (StringUtils.equals(param.getParamKey(), ParamConstants.MAIL.TLS.getValue())) {
mailInfo.setTls(param.getParamValue());
} else if (StringUtils.equals(param.getParamKey(), ParamConstants.MAIL.RECIPIENTS.getValue())) {
mailInfo.setRecipient(param.getParamValue());
}
systemParameter.setParamKey(value.getKey());
systemParameter.setSort(value.getValue());
paramList.add(systemParameter);
}
} else {
paramList.stream().filter(param -> param.getParamKey().equals(ParamConstants.MAIL.PASSWORD.getKey())).forEach(param -> {
if (!StringUtils.isBlank(param.getParamValue())) {
String string = EncryptUtils.aesDecrypt(param.getParamValue()).toString();
param.setParamValue(string);
}
});
}
paramList.sort(Comparator.comparingInt(SystemParameter::getSort));
return paramList;
return mailInfo;
}
public void saveLdap(List<SystemParameter> parameters) {

View File

@ -36,17 +36,27 @@
</el-form-item>
</el-col>
</el-row>
<el-row>
<el-col>
<el-form-item :label="$t('system_parameter_setting.test_recipients')">
<el-input v-model="formInline.recipient" :placeholder="$t('system_parameter_setting.test_recipients')"
autocomplete="new-password" show-password type="text" ref="input">
</el-input>
<p style="color: #8a8b8d">({{ $t('system_parameter_setting.tip') }})</p>
</el-form-item>
</el-col>
</el-row>
<!---->
<div style="border: 0px;margin-bottom: 20px;margin-top: 20px">
<el-checkbox v-model="formInline.SSL" :label="$t('system_parameter_setting.SSL')"></el-checkbox>
<el-checkbox v-model="formInline.ssl" :label="$t('system_parameter_setting.SSL')"></el-checkbox>
</div>
<div style="border: 0px;margin-bottom: 20px">
<el-checkbox v-model="formInline.TLS" :label="$t('system_parameter_setting.TLS')"></el-checkbox>
<el-checkbox v-model="formInline.tls" :label="$t('system_parameter_setting.TLS')"></el-checkbox>
</div>
<div style="border: 0px;margin-bottom: 20px">
<!-- <div style="border: 0px;margin-bottom: 20px">
<el-checkbox v-model="formInline.ANON" :label="$t('system_parameter_setting.SMTP')"></el-checkbox>
</div>
</div>-->
<template v-slot:footer>
</template>
</el-form>
@ -114,13 +124,10 @@ export default {
},
query() {
this.result = this.$get("/system/mail/info", response => {
this.$set(this.formInline, "host", response.data[0].paramValue);
this.$set(this.formInline, "port", response.data[1].paramValue);
this.$set(this.formInline, "account", response.data[2].paramValue);
this.$set(this.formInline, "password", response.data[3].paramValue);
this.$set(this.formInline, "SSL", JSON.parse(response.data[4].paramValue));
this.$set(this.formInline, "TLS", JSON.parse(response.data[5].paramValue));
this.$set(this.formInline, "ANON", JSON.parse(response.data[6].paramValue));
this.formInline = response.data;
this.formInline.ssl = this.formInline.ssl === 'true';
this.formInline.tls = this.formInline.tls === 'true';
console.log(this.formInline)
this.$nextTick(() => {
this.$refs.formInline.clearValidate();
})
@ -137,13 +144,13 @@ export default {
},
testConnection(formInline) {
let param = {
"smtp.server": this.formInline.host,
"smtp.host": this.formInline.host,
"smtp.port": this.formInline.port,
"smtp.account": this.formInline.account,
"smtp.password": this.formInline.password,
"smtp.ssl": this.formInline.SSL,
"smtp.tls": this.formInline.TLS,
"smtp.anon": this.formInline.ANON,
"smtp.ssl": this.formInline.ssl,
"smtp.tls": this.formInline.tls,
"smtp.recipient": this.formInline.recipient,
};
this.$refs[formInline].validate((valid) => {
if (valid) {
@ -171,9 +178,10 @@ export default {
{paramKey: "smtp.port", paramValue: this.formInline.port, type: "text", sort: 2},
{paramKey: "smtp.account", paramValue: this.formInline.account, type: "text", sort: 3},
{paramKey: "smtp.password", paramValue: this.formInline.password, type: "password", sort: 4},
{paramKey: "smtp.ssl", paramValue: this.formInline.SSL, type: "text", sort: 5},
{paramKey: "smtp.tls", paramValue: this.formInline.TLS, type: "text", sort: 6},
{paramKey: "smtp.anon", paramValue: this.formInline.ANON, type: "text", sort: 7}
{paramKey: "smtp.ssl", paramValue: this.formInline.ssl, type: "text", sort: 5},
{paramKey: "smtp.tls", paramValue: this.formInline.tls, type: "text", sort: 6},
{paramKey: "smtp.recipient", paramValue: this.formInline.recipient, type: "text", sort: 8}
]
this.$refs[formInline].validate(valid => {

View File

@ -1252,6 +1252,8 @@ export default {
host: 'Host number cannot be empty',
port: 'Port cannot be empty',
account: 'Account cannot be empty',
test_recipients:'Test recipients',
tip:'Tip: use as test mail recipient only',
},
i18n: {

View File

@ -1253,6 +1253,8 @@ export default {
host: '主机号不能为空',
port: '端口号不能为空',
account: '账户不能为空',
test_recipients:'测试收件人',
tip:'提示:仅用来作为测试邮件收件人',
},
i18n: {
home: '首页',

View File

@ -1252,6 +1252,8 @@ export default {
host: '主機號不能為空',
port: '端口號不能為空',
account: '賬戶不能為空',
test_recipients:'測試收件人',
tip:'提示:僅用來作為測試郵件收件人',
},
i18n: {
home: '首頁',