Skip to content

Commit

Permalink
feat: ✨ 新增邮件模块
Browse files Browse the repository at this point in the history
  • Loading branch information
fxbin committed Sep 7, 2023
1 parent c0e08ea commit 525a71d
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 0 deletions.
20 changes: 20 additions & 0 deletions bubble-dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
<mica-auto.version>2.3.2</mica-auto.version>
<easy-trans.version>2.2.6</easy-trans.version>
<dubbo.version>3.1.11</dubbo.version>
<sms4j.version>2.2.0</sms4j.version>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -144,6 +145,12 @@
<version>2.0.0.BUILD-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>cn.fxbin.bubble</groupId>
<artifactId>bubble-spring-boot-starter-mail</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
</dependency>

<!-- Spring Boot POM -->
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down Expand Up @@ -464,6 +471,19 @@
</exclusions>
</dependency>


<!-- sms4j https://wind.kim/ -->
<dependency>
<groupId>org.dromara.sms4j</groupId>
<artifactId>sms4j-Email-api</artifactId>
<version>${sms4j.version}</version>
</dependency>
<dependency>
<groupId>org.dromara.sms4j</groupId>
<artifactId>sms4j-Email-core</artifactId>
<version>${sms4j.version}</version>
</dependency>

</dependencies>
</dependencyManagement>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>cn.fxbin.bubble</groupId>
<artifactId>bubble-spring-boot-starters</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
</parent>

<artifactId>bubble-spring-boot-starter-mail</artifactId>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>cn.fxbin.bubble</groupId>
<artifactId>bubble-spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>org.dromara.sms4j</groupId>
<artifactId>sms4j-Email-core</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -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<String, MailSmtpConfig> tenant = Maps.newHashMap();

}
Original file line number Diff line number Diff line change
@@ -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<String, MailSmtpConfig> tenantConfig = properties.getTenant();
tenantConfig.forEach(MailFactory::put);
}
}
}
1 change: 1 addition & 0 deletions bubble-spring-boot-starters/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
<module>bubble-spring-boot-starter-xxl-job</module>
<module>bubble-spring-boot-starter-dynamic-threadpool</module>
<module>bubble-spring-boot-starter-i18n</module>
<module>bubble-spring-boot-starter-mail</module>
</modules>


Expand Down

0 comments on commit 525a71d

Please sign in to comment.