refactor: 邮件配置增加发件人设置

This commit is contained in:
CaptainB 2022-03-29 16:34:22 +08:00 committed by 刘瑞斌
parent 56a30bc4f9
commit df070ffdc7
8 changed files with 39 additions and 8 deletions

View File

@ -92,6 +92,7 @@ public interface ParamConstants {
SERVER("smtp.host"),
PORT("smtp.port"),
ACCOUNT("smtp.account"),
FROM("smtp.from"),
PASSWORD("smtp.password"),
SSL("smtp.ssl"),
TLS("smtp.tls"),

View File

@ -7,6 +7,7 @@ public class MailInfo {
private String host;
private String port;
private String account;
private String from;
private String password;
private String ssl;
private String tls;

View File

@ -32,6 +32,7 @@ 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.HashMap;
import java.util.List;
@ -104,7 +105,7 @@ public class SystemParameterService {
JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
javaMailSender.setDefaultEncoding("UTF-8");
javaMailSender.setHost(hashMap.get(ParamConstants.MAIL.SERVER.getValue()));
javaMailSender.setPort(Integer.valueOf(hashMap.get(ParamConstants.MAIL.PORT.getValue())));
javaMailSender.setPort(Integer.parseInt(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();
@ -131,18 +132,26 @@ public class SystemParameterService {
MimeMessageHelper helper = null;
try {
helper = new MimeMessageHelper(mimeMessage, true);
if (javaMailSender.getUsername().contains("@")) {
helper.setFrom(javaMailSender.getUsername());
String username = javaMailSender.getUsername();
String email;
if (username.contains("@")) {
email = username;
} else {
String mailHost = javaMailSender.getHost();
String domainName = mailHost.substring(mailHost.indexOf(".") + 1, mailHost.length());
helper.setFrom(javaMailSender.getUsername() + "@" + domainName);
String domainName = mailHost.substring(mailHost.indexOf(".") + 1);
email = username + "@" + domainName;
}
InternetAddress from = new InternetAddress();
from.setAddress(email);
from.setPersonal(hashMap.getOrDefault(ParamConstants.MAIL.FROM.getValue(), username));
helper.setFrom(from);
helper.setSubject("MeterSphere测试邮件 ");
helper.setText("这是一封测试邮件,邮件发送成功", true);
helper.setTo(recipients);
javaMailSender.send(mimeMessage);
} catch (MessagingException e) {
} catch (Exception e) {
LogUtil.error(e.getMessage(), e);
MSException.throwException(Translator.get("connection_failed"));
}
@ -166,6 +175,8 @@ public class SystemParameterService {
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.FROM.getValue())) {
mailInfo.setFrom(param.getParamValue());
} else if (StringUtils.equals(param.getParamKey(), ParamConstants.MAIL.PASSWORD.getValue())) {
String password = EncryptUtils.aesDecrypt(param.getParamValue()).toString();
mailInfo.setPassword(password);

View File

@ -104,3 +104,9 @@ CREATE TABLE `ui_scenario_reference` (
-- module management
INSERT INTO system_parameter (param_key, param_value, type, sort)
VALUES ('metersphere.module.ui', 'ENABLE', 'text', 1);
-- 邮件默认发件人
INSERT INTO system_parameter
SELECT 'smtp.from', param_value, type, sort
FROM system_parameter
WHERE param_key = 'smtp.account';

View File

@ -36,6 +36,15 @@
</el-form-item>
</el-col>
</el-row>
<el-row>
<el-col>
<el-form-item :label="$t('system_parameter_setting.SMTP_from')" prop="from">
<el-input v-model="formInline.from" :placeholder="$t('system_parameter_setting.SMTP_from')"
type="text" v-on:input="change()">
</el-input>
</el-form-item>
</el-col>
</el-row>
<el-row>
<el-col>
<el-form-item :label="$t('system_parameter_setting.test_recipients')">
@ -144,6 +153,7 @@ export default {
"smtp.port": this.formInline.port,
"smtp.account": this.formInline.account,
"smtp.password": this.formInline.password,
"smtp.from": this.formInline.from,
"smtp.ssl": this.formInline.ssl,
"smtp.tls": this.formInline.tls,
"smtp.recipient": this.formInline.recipient,
@ -173,11 +183,11 @@ export default {
{paramKey: "smtp.host", paramValue: this.formInline.host, type: "text", sort: 1},
{paramKey: "smtp.port", paramValue: this.formInline.port, type: "text", sort: 2},
{paramKey: "smtp.account", paramValue: this.formInline.account, type: "text", sort: 3},
{paramKey: "smtp.from", paramValue: this.formInline.from, type: "text", 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.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

@ -2452,6 +2452,7 @@ export default {
SMTP_port: 'SMTP port',
SMTP_account: 'SMTP account',
SMTP_password: 'SMTP password',
SMTP_from: 'From',
SSL: 'Turn on SSL (if the SMTP port is 465, you usually need to enable SSL)',
TLS: 'Turn on TLS (if the SMTP port is 587, you usually need to enable TLS)',
SMTP: 'Anonymous SMTP or not',
@ -2460,7 +2461,6 @@ export default {
account: 'Account cannot be empty',
test_recipients: 'Test recipients',
tip: 'Tip: use as test mail recipient only',
},
i18n: {
home: 'Home',

View File

@ -2456,6 +2456,7 @@ export default {
SMTP_port: 'SMTP端口',
SMTP_account: 'SMTP账户',
SMTP_password: 'SMTP密码',
SMTP_from: '指定发件人',
SSL: '开启SSL(如果SMTP端口是465通常需要启用SSL)',
TLS: '开启TLS(如果SMTP端口是587通常需要启用TLS)',
SMTP: '是否免密 SMTP',

View File

@ -2455,6 +2455,7 @@ export default {
SMTP_port: 'SMTP端口',
SMTP_account: 'SMTP賬戶',
SMTP_password: 'SMTP密碼',
SMTP_from: '指定发件人',
SSL: '開啟SSL(如果SMTP端口是465通常需要啟用SSL)',
TLS: '開啟TLS(如果SMTP端口是587通常需要啟用TLS)',
SMTP: '是否免密 SMTP',