From df070ffdc77cd182a0fdbde82b60399da83f6e6a Mon Sep 17 00:00:00 2001 From: CaptainB Date: Tue, 29 Mar 2022 16:34:22 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=82=AE=E4=BB=B6=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E5=A2=9E=E5=8A=A0=E5=8F=91=E4=BB=B6=E4=BA=BA=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../commons/constants/ParamConstants.java | 1 + .../metersphere/notice/domain/MailInfo.java | 1 + .../service/SystemParameterService.java | 23 ++++++++++++++----- .../db/migration/V115__1.20__release.sql | 6 +++++ .../settings/system/EmailSetting.vue | 12 +++++++++- frontend/src/i18n/en-US.js | 2 +- frontend/src/i18n/zh-CN.js | 1 + frontend/src/i18n/zh-TW.js | 1 + 8 files changed, 39 insertions(+), 8 deletions(-) diff --git a/backend/src/main/java/io/metersphere/commons/constants/ParamConstants.java b/backend/src/main/java/io/metersphere/commons/constants/ParamConstants.java index 552527c378..a7d2a3c7de 100644 --- a/backend/src/main/java/io/metersphere/commons/constants/ParamConstants.java +++ b/backend/src/main/java/io/metersphere/commons/constants/ParamConstants.java @@ -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"), diff --git a/backend/src/main/java/io/metersphere/notice/domain/MailInfo.java b/backend/src/main/java/io/metersphere/notice/domain/MailInfo.java index 023f730734..d9f020d885 100644 --- a/backend/src/main/java/io/metersphere/notice/domain/MailInfo.java +++ b/backend/src/main/java/io/metersphere/notice/domain/MailInfo.java @@ -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; diff --git a/backend/src/main/java/io/metersphere/service/SystemParameterService.java b/backend/src/main/java/io/metersphere/service/SystemParameterService.java index 25b864a93f..e707285a07 100644 --- a/backend/src/main/java/io/metersphere/service/SystemParameterService.java +++ b/backend/src/main/java/io/metersphere/service/SystemParameterService.java @@ -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); diff --git a/backend/src/main/resources/db/migration/V115__1.20__release.sql b/backend/src/main/resources/db/migration/V115__1.20__release.sql index a86efe0d93..4cf95431fc 100644 --- a/backend/src/main/resources/db/migration/V115__1.20__release.sql +++ b/backend/src/main/resources/db/migration/V115__1.20__release.sql @@ -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'; \ No newline at end of file diff --git a/frontend/src/business/components/settings/system/EmailSetting.vue b/frontend/src/business/components/settings/system/EmailSetting.vue index fd8e02dfd7..b268cc6095 100644 --- a/frontend/src/business/components/settings/system/EmailSetting.vue +++ b/frontend/src/business/components/settings/system/EmailSetting.vue @@ -36,6 +36,15 @@ + + + + + + + + @@ -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 => { diff --git a/frontend/src/i18n/en-US.js b/frontend/src/i18n/en-US.js index 8dd964302b..a19fd19d11 100644 --- a/frontend/src/i18n/en-US.js +++ b/frontend/src/i18n/en-US.js @@ -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', diff --git a/frontend/src/i18n/zh-CN.js b/frontend/src/i18n/zh-CN.js index d2d1c55de6..8fc94cb72f 100644 --- a/frontend/src/i18n/zh-CN.js +++ b/frontend/src/i18n/zh-CN.js @@ -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', diff --git a/frontend/src/i18n/zh-TW.js b/frontend/src/i18n/zh-TW.js index 38fb568af7..61b749e3f6 100644 --- a/frontend/src/i18n/zh-TW.js +++ b/frontend/src/i18n/zh-TW.js @@ -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',