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"), SERVER("smtp.host"),
PORT("smtp.port"), PORT("smtp.port"),
ACCOUNT("smtp.account"), ACCOUNT("smtp.account"),
FROM("smtp.from"),
PASSWORD("smtp.password"), PASSWORD("smtp.password"),
SSL("smtp.ssl"), SSL("smtp.ssl"),
TLS("smtp.tls"), TLS("smtp.tls"),

View File

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

View File

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

View File

@ -104,3 +104,9 @@ CREATE TABLE `ui_scenario_reference` (
-- module management -- module management
INSERT INTO system_parameter (param_key, param_value, type, sort) INSERT INTO system_parameter (param_key, param_value, type, sort)
VALUES ('metersphere.module.ui', 'ENABLE', 'text', 1); 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-form-item>
</el-col> </el-col>
</el-row> </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-row>
<el-col> <el-col>
<el-form-item :label="$t('system_parameter_setting.test_recipients')"> <el-form-item :label="$t('system_parameter_setting.test_recipients')">
@ -144,6 +153,7 @@ export default {
"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.from": this.formInline.from,
"smtp.ssl": this.formInline.ssl, "smtp.ssl": this.formInline.ssl,
"smtp.tls": this.formInline.tls, "smtp.tls": this.formInline.tls,
"smtp.recipient": this.formInline.recipient, "smtp.recipient": this.formInline.recipient,
@ -173,11 +183,11 @@ export default {
{paramKey: "smtp.host", paramValue: this.formInline.host, type: "text", sort: 1}, {paramKey: "smtp.host", paramValue: this.formInline.host, type: "text", sort: 1},
{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.from", paramValue: this.formInline.from, type: "text", sort: 4},
{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.recipient", paramValue: this.formInline.recipient, type: "text", sort: 8} {paramKey: "smtp.recipient", paramValue: this.formInline.recipient, type: "text", sort: 8}
] ]
this.$refs[formInline].validate(valid => { this.$refs[formInline].validate(valid => {

View File

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

View File

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

View File

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