From 525a71df81a6b74180cb19ba14efd093877bb300 Mon Sep 17 00:00:00 2001 From: fxbin Date: Thu, 7 Sep 2023 20:31:23 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20:sparkles:=20=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E9=82=AE=E4=BB=B6=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bubble-dependencies/pom.xml | 20 +++++ .../bubble-spring-boot-starter-mail/pom.xml | 32 ++++++++ .../cn/fxbin/bubble/mail/MailProperties.java | 76 +++++++++++++++++++ .../autoconfigure/MailAutoConfiguration.java | 69 +++++++++++++++++ bubble-spring-boot-starters/pom.xml | 1 + 5 files changed, 198 insertions(+) create mode 100644 bubble-spring-boot-starters/bubble-spring-boot-starter-mail/pom.xml create mode 100644 bubble-spring-boot-starters/bubble-spring-boot-starter-mail/src/main/java/cn/fxbin/bubble/mail/MailProperties.java create mode 100644 bubble-spring-boot-starters/bubble-spring-boot-starter-mail/src/main/java/cn/fxbin/bubble/mail/autoconfigure/MailAutoConfiguration.java diff --git a/bubble-dependencies/pom.xml b/bubble-dependencies/pom.xml index 23216202..50cf089e 100644 --- a/bubble-dependencies/pom.xml +++ b/bubble-dependencies/pom.xml @@ -48,6 +48,7 @@ 2.3.2 2.2.6 3.1.11 + 2.2.0 @@ -144,6 +145,12 @@ 2.0.0.BUILD-SNAPSHOT + + cn.fxbin.bubble + bubble-spring-boot-starter-mail + 2.0.0.BUILD-SNAPSHOT + + org.springframework.boot @@ -464,6 +471,19 @@ + + + + org.dromara.sms4j + sms4j-Email-api + ${sms4j.version} + + + org.dromara.sms4j + sms4j-Email-core + ${sms4j.version} + + diff --git a/bubble-spring-boot-starters/bubble-spring-boot-starter-mail/pom.xml b/bubble-spring-boot-starters/bubble-spring-boot-starter-mail/pom.xml new file mode 100644 index 00000000..c7d3733f --- /dev/null +++ b/bubble-spring-boot-starters/bubble-spring-boot-starter-mail/pom.xml @@ -0,0 +1,32 @@ + + + 4.0.0 + + cn.fxbin.bubble + bubble-spring-boot-starters + 2.0.0.BUILD-SNAPSHOT + + + bubble-spring-boot-starter-mail + + + 17 + 17 + UTF-8 + + + + + cn.fxbin.bubble + bubble-spring-boot-starter + + + + org.dromara.sms4j + sms4j-Email-core + + + + \ No newline at end of file diff --git a/bubble-spring-boot-starters/bubble-spring-boot-starter-mail/src/main/java/cn/fxbin/bubble/mail/MailProperties.java b/bubble-spring-boot-starters/bubble-spring-boot-starter-mail/src/main/java/cn/fxbin/bubble/mail/MailProperties.java new file mode 100644 index 00000000..984004e2 --- /dev/null +++ b/bubble-spring-boot-starters/bubble-spring-boot-starter-mail/src/main/java/cn/fxbin/bubble/mail/MailProperties.java @@ -0,0 +1,76 @@ +package cn.fxbin.bubble.mail; + +import com.google.common.collect.Maps; +import lombok.Data; +import org.dromara.email.comm.config.MailSmtpConfig; +import org.springframework.boot.context.properties.ConfigurationProperties; + +import java.util.Map; + +import static cn.fxbin.bubble.mail.MailProperties.BUBBLE_MAIL_PREFIX; + +/** + * MailProperties + * + * @author fxbin + * @version v1.0 + * @since 2023/9/7 19:33 + */ +@Data +@ConfigurationProperties(prefix = BUBBLE_MAIL_PREFIX) +public class MailProperties { + + public static final String BUBBLE_MAIL_PREFIX = "bubble.mail"; + + + /** + * 是否开启 mail,默认:true + */ + private boolean enabled = true; + + /** + * 端口号 + * */ + private String port; + + /** + * 发件人地址 + * */ + private String fromAddress; + + /** + * 服务器地址 + * */ + private String smtpServer; + + /** + * 账号 + * */ + private String username; + + /** + * 密码 + * */ + private String password; + + /** + * 是否开启ssl 默认开启 + * */ + private String isSsl = "true"; + + /** + * 是否开启验证 默认开启 + * */ + private String isAuth = "true"; + + /** + * 多租户配置时需要配置此项,指定默认主租户 + */ + private String primary; + + /** + * 多租户配置 + */ + private Map tenant = Maps.newHashMap(); + +} \ No newline at end of file diff --git a/bubble-spring-boot-starters/bubble-spring-boot-starter-mail/src/main/java/cn/fxbin/bubble/mail/autoconfigure/MailAutoConfiguration.java b/bubble-spring-boot-starters/bubble-spring-boot-starter-mail/src/main/java/cn/fxbin/bubble/mail/autoconfigure/MailAutoConfiguration.java new file mode 100644 index 00000000..64b46a00 --- /dev/null +++ b/bubble-spring-boot-starters/bubble-spring-boot-starter-mail/src/main/java/cn/fxbin/bubble/mail/autoconfigure/MailAutoConfiguration.java @@ -0,0 +1,69 @@ +package cn.fxbin.bubble.mail.autoconfigure; + +import cn.fxbin.bubble.core.util.CollectionUtils; +import cn.fxbin.bubble.mail.MailProperties; +import jakarta.annotation.Resource; +import org.dromara.email.api.MailClient; +import org.dromara.email.comm.config.MailSmtpConfig; +import org.dromara.email.core.factory.MailFactory; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Lazy; +import org.springframework.util.Assert; + +import java.util.Map; + +/** + * MailAutoConfiguration + * + * @author fxbin + * @version v1.0 + * @since 2023/9/7 19:38 + */ +@Configuration( + proxyBeanMethods = false +) +@ConditionalOnProperty(prefix = MailProperties.BUBBLE_MAIL_PREFIX, name = "enabled", havingValue = "true") +@EnableConfigurationProperties(MailProperties.class) +public class MailAutoConfiguration implements InitializingBean { + + @Resource + private MailProperties properties; + + @Bean + @Lazy + public MailClient mailClient() { + MailSmtpConfig mailSmtpConfig = MailSmtpConfig.builder() + .port(properties.getPort()) + .smtpServer(properties.getSmtpServer()) + .username(properties.getUsername()) + .password(properties.getPassword()) + .fromAddress(properties.getFromAddress()) + .isSSL(properties.getIsSsl()) + .isAuth(properties.getIsAuth()) + .build(); + MailFactory.put("default", mailSmtpConfig); + return MailFactory.createMailClient("default"); + } + + /** + * @throws Exception + */ + @Override + public void afterPropertiesSet() throws Exception { + // 多租户配置 + if (properties.isEnabled() && CollectionUtils.isNotEmpty(properties.getTenant())) { + + + Assert.isNull(properties.getPrimary(), "bubble.mail.primary 属性不允许为空"); + Assert.isTrue(properties.getTenant().containsKey(properties.getPrimary()), + "bubble.mail.tenant 属性配置未包含 bubble.mail.primary 指定value"); + + Map tenantConfig = properties.getTenant(); + tenantConfig.forEach(MailFactory::put); + } + } +} \ No newline at end of file diff --git a/bubble-spring-boot-starters/pom.xml b/bubble-spring-boot-starters/pom.xml index e5009b2a..b43bdc47 100644 --- a/bubble-spring-boot-starters/pom.xml +++ b/bubble-spring-boot-starters/pom.xml @@ -37,6 +37,7 @@ bubble-spring-boot-starter-xxl-job bubble-spring-boot-starter-dynamic-threadpool bubble-spring-boot-starter-i18n + bubble-spring-boot-starter-mail