feat(messaging/mail): 邮件支持自定义发件人

Closes #5
This commit is contained in:
2024-06-18 23:27:18 +08:00
parent c84539b461
commit 27b949dd9d
2 changed files with 16 additions and 1 deletions

View File

@@ -62,6 +62,11 @@ public class MailConfig {
*/ */
private String password; private String password;
/**
* 发件人
*/
private String from;
/** /**
* 是否启用 SSL 连接 * 是否启用 SSL 连接
*/ */
@@ -121,6 +126,14 @@ public class MailConfig {
this.password = password; this.password = password;
} }
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public boolean isSslEnabled() { public boolean isSslEnabled() {
return sslEnabled; return sslEnabled;
} }
@@ -157,6 +170,7 @@ public class MailConfig {
public Properties toJavaMailProperties() { public Properties toJavaMailProperties() {
Properties javaMailProperties = new Properties(); Properties javaMailProperties = new Properties();
javaMailProperties.putAll(this.getProperties()); javaMailProperties.putAll(this.getProperties());
javaMailProperties.put("mail.from", this.getFrom());
javaMailProperties.put("mail.smtp.auth", true); javaMailProperties.put("mail.smtp.auth", true);
javaMailProperties.put("mail.smtp.ssl.enable", this.isSslEnabled()); javaMailProperties.put("mail.smtp.ssl.enable", this.isSslEnabled());
if (this.isSslEnabled()) { if (this.isSslEnabled()) {

View File

@@ -164,7 +164,8 @@ public class MailUtils {
MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, StandardCharsets.UTF_8 MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, StandardCharsets.UTF_8
.displayName()); .displayName());
// 设置基本信息 // 设置基本信息
messageHelper.setFrom(mailSender.getUsername()); messageHelper.setFrom(CharSequenceUtil.blankToDefault(mailSender.getJavaMailProperties()
.getProperty("mail.from"), mailSender.getUsername()));
messageHelper.setSubject(subject); messageHelper.setSubject(subject);
messageHelper.setText(content, isHtml); messageHelper.setText(content, isHtml);
// 设置收信人 // 设置收信人