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

View File

@ -5,6 +5,7 @@ import io.metersphere.commons.constants.ParamConstants;
import io.metersphere.commons.constants.RoleConstants; import io.metersphere.commons.constants.RoleConstants;
import io.metersphere.dto.BaseSystemConfigDTO; import io.metersphere.dto.BaseSystemConfigDTO;
import io.metersphere.ldap.domain.LdapInfo; import io.metersphere.ldap.domain.LdapInfo;
import io.metersphere.notice.domain.MailInfo;
import io.metersphere.service.SystemParameterService; import io.metersphere.service.SystemParameterService;
import org.apache.shiro.authz.annotation.RequiresRoles; import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
@ -38,7 +39,7 @@ public class SystemParameterController {
@GetMapping("/mail/info") @GetMapping("/mail/info")
@RequiresRoles(value = {RoleConstants.ADMIN}) @RequiresRoles(value = {RoleConstants.ADMIN})
public Object mailInfo() { public MailInfo mailInfo() {
return SystemParameterService.mailInfo(ParamConstants.Classify.MAIL.getValue()); 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.dto.BaseSystemConfigDTO;
import io.metersphere.i18n.Translator; import io.metersphere.i18n.Translator;
import io.metersphere.ldap.domain.LdapInfo; import io.metersphere.ldap.domain.LdapInfo;
import io.metersphere.notice.domain.MailInfo;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.BooleanUtils; import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.mail.MessagingException; import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.*; import java.util.*;
@ -56,7 +59,7 @@ public class SystemParameterService {
parameters.forEach(parameter -> { parameters.forEach(parameter -> {
SystemParameterExample example = new SystemParameterExample(); 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())) { if (!StringUtils.isBlank(parameter.getParamValue())) {
String string = EncryptUtils.aesEncrypt(parameter.getParamValue()).toString(); String string = EncryptUtils.aesEncrypt(parameter.getParamValue()).toString();
parameter.setParamValue(string); parameter.setParamValue(string);
@ -82,23 +85,16 @@ public class SystemParameterService {
public void testConnection(HashMap<String, String> hashMap) { public void testConnection(HashMap<String, String> hashMap) {
JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl(); JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
javaMailSender.setDefaultEncoding("UTF-8"); javaMailSender.setDefaultEncoding("UTF-8");
javaMailSender.setHost(hashMap.get(ParamConstants.MAIL.SERVER.getKey())); javaMailSender.setHost(hashMap.get(ParamConstants.MAIL.SERVER.getValue()));
javaMailSender.setPort(Integer.valueOf(hashMap.get(ParamConstants.MAIL.PORT.getKey()))); javaMailSender.setPort(Integer.valueOf(hashMap.get(ParamConstants.MAIL.PORT.getValue())));
javaMailSender.setUsername(hashMap.get(ParamConstants.MAIL.ACCOUNT.getKey())); javaMailSender.setUsername(hashMap.get(ParamConstants.MAIL.ACCOUNT.getValue()));
javaMailSender.setPassword(hashMap.get(ParamConstants.MAIL.PASSWORD.getKey())); javaMailSender.setPassword(hashMap.get(ParamConstants.MAIL.PASSWORD.getValue()));
Properties props = new Properties(); Properties props = new Properties();
boolean isAnon = Boolean.parseBoolean(hashMap.get(ParamConstants.MAIL.ANON.getKey())); String recipients = hashMap.get(ParamConstants.MAIL.RECIPIENTS.getValue());
if (isAnon) { if (BooleanUtils.toBoolean(hashMap.get(ParamConstants.MAIL.SSL.getValue()))) {
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()))) {
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 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.starttls.enable", "true");
} }
props.put("mail.smtp.timeout", "30000"); props.put("mail.smtp.timeout", "30000");
@ -110,39 +106,53 @@ public class SystemParameterService {
LogUtil.error(e.getMessage(), e); LogUtil.error(e.getMessage(), e);
MSException.throwException(Translator.get("connection_failed")); 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() { public String getVersion() {
return System.getenv("MS_VERSION"); return System.getenv("MS_VERSION");
} }
public Object mailInfo(String type) { public MailInfo mailInfo(String type) {
List<SystemParameter> paramList = this.getParamList(type); List<SystemParameter> paramList = this.getParamList(type);
if (CollectionUtils.isEmpty(paramList)) { MailInfo mailInfo=new MailInfo ();
paramList = new ArrayList<>(); if (!CollectionUtils.isEmpty(paramList)) {
ParamConstants.MAIL[] values = ParamConstants.MAIL.values(); for (SystemParameter param : paramList) {
for (ParamConstants.MAIL value : values) { if (StringUtils.equals(param.getParamKey(),ParamConstants.MAIL.SERVER.getValue() )) {
SystemParameter systemParameter = new SystemParameter(); mailInfo.setHost(param.getParamValue());
if (value.equals(ParamConstants.MAIL.PASSWORD)) { } else if (StringUtils.equals(param.getParamKey(), ParamConstants.MAIL.PORT.getValue())) {
systemParameter.setType(ParamConstants.Type.PASSWORD.getValue()); mailInfo.setPort(param.getParamValue());
} else { } else if (StringUtils.equals(param.getParamKey(), ParamConstants.MAIL.ACCOUNT.getValue())) {
systemParameter.setType(ParamConstants.Type.TEXT.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 mailInfo;
return paramList;
} }
public void saveLdap(List<SystemParameter> parameters) { public void saveLdap(List<SystemParameter> parameters) {

View File

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

View File

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

View File

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

View File

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