mirror of
https://github.com/continew-org/continew-starter.git
synced 2025-10-15 00:57:11 +08:00
refactor(messaging/mail): 提供 JavaMailSenderImpl 默认配置,并重构 MailConfigurer 配置代码
This commit is contained in:
@@ -16,12 +16,18 @@
|
||||
|
||||
package top.continew.starter.messaging.mail.autoconfigure;
|
||||
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.mail.MailProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.mail.javamail.JavaMailSenderImpl;
|
||||
import top.continew.starter.core.util.GeneralPropertySourceFactory;
|
||||
import top.continew.starter.core.util.MapUtils;
|
||||
|
||||
/**
|
||||
* 邮件自动配置
|
||||
@@ -30,11 +36,43 @@ import top.continew.starter.core.util.GeneralPropertySourceFactory;
|
||||
* @since 1.0.0
|
||||
*/
|
||||
@AutoConfiguration
|
||||
@EnableConfigurationProperties(MailProperties.class)
|
||||
@PropertySource(value = "classpath:default-messaging-mail.yml", factory = GeneralPropertySourceFactory.class)
|
||||
public class MailAutoConfiguration {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(MailAutoConfiguration.class);
|
||||
|
||||
@Bean
|
||||
JavaMailSenderImpl mailSender(MailProperties properties) {
|
||||
JavaMailSenderImpl sender = new JavaMailSenderImpl();
|
||||
this.applyProperties(properties, sender);
|
||||
return sender;
|
||||
}
|
||||
|
||||
/**
|
||||
* 应用邮件配置
|
||||
*
|
||||
* @param properties 邮件配置
|
||||
* @param sender 邮件 Sender
|
||||
*/
|
||||
private void applyProperties(MailProperties properties, JavaMailSenderImpl sender) {
|
||||
sender.setHost(properties.getHost());
|
||||
if (properties.getPort() != null) {
|
||||
sender.setPort(properties.getPort());
|
||||
}
|
||||
|
||||
sender.setUsername(properties.getUsername());
|
||||
sender.setPassword(properties.getPassword());
|
||||
sender.setProtocol(properties.getProtocol());
|
||||
if (properties.getDefaultEncoding() != null) {
|
||||
sender.setDefaultEncoding(properties.getDefaultEncoding().name());
|
||||
}
|
||||
|
||||
if (MapUtil.isNotEmpty(properties.getProperties())) {
|
||||
sender.setJavaMailProperties(MapUtils.toProperties(properties.getProperties()));
|
||||
}
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void postConstruct() {
|
||||
log.debug("[ContiNew Starter] - Auto Configuration 'Mail' completed initialization.");
|
||||
|
@@ -17,12 +17,10 @@
|
||||
package top.continew.starter.messaging.mail.core;
|
||||
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import top.continew.starter.core.util.validation.ValidationUtils;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* 邮件配置
|
||||
@@ -162,25 +160,6 @@ public class MailConfig {
|
||||
return properties;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将当前配置转换为 JavaMail 的 Properties 对象
|
||||
*
|
||||
* @return Properties 对象
|
||||
*/
|
||||
public Properties toJavaMailProperties() {
|
||||
Properties javaMailProperties = new Properties();
|
||||
javaMailProperties.putAll(this.getProperties());
|
||||
javaMailProperties.put("mail.from", this.getFrom());
|
||||
javaMailProperties.put("mail.smtp.auth", true);
|
||||
javaMailProperties.put("mail.smtp.ssl.enable", this.isSslEnabled());
|
||||
if (this.isSslEnabled()) {
|
||||
ValidationUtils.throwIfNull(this.getSslPort(), "邮件配置不正确:SSL端口不能为空");
|
||||
javaMailProperties.put("mail.smtp.socketFactory.port", this.sslPort);
|
||||
javaMailProperties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
|
||||
}
|
||||
return javaMailProperties;
|
||||
}
|
||||
|
||||
static {
|
||||
DEFAULT_CHARSET = StandardCharsets.UTF_8;
|
||||
}
|
||||
|
@@ -19,6 +19,8 @@ package top.continew.starter.messaging.mail.core;
|
||||
import org.springframework.mail.javamail.JavaMailSenderImpl;
|
||||
import top.continew.starter.core.util.validation.ValidationUtils;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* 邮件配置
|
||||
*
|
||||
@@ -62,8 +64,17 @@ public interface MailConfigurer {
|
||||
sender.setDefaultEncoding(mailConfig.getDefaultEncoding().name());
|
||||
}
|
||||
|
||||
Properties javaMailProperties = new Properties();
|
||||
if (!mailConfig.getProperties().isEmpty()) {
|
||||
sender.setJavaMailProperties(mailConfig.toJavaMailProperties());
|
||||
javaMailProperties.putAll(mailConfig.getProperties());
|
||||
javaMailProperties.put("mail.from", mailConfig.getFrom());
|
||||
}
|
||||
javaMailProperties.put("mail.smtp.auth", true);
|
||||
if (mailConfig.isSslEnabled()) {
|
||||
ValidationUtils.throwIfNull(mailConfig.getSslPort(), "邮件配置不正确:SSL端口不能为空");
|
||||
javaMailProperties.put("mail.smtp.socketFactory.port", mailConfig.getSslPort());
|
||||
javaMailProperties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
|
||||
}
|
||||
sender.setJavaMailProperties(javaMailProperties);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user