feat: add mail module
This commit is contained in:
15
pom.xml
15
pom.xml
@@ -35,6 +35,7 @@
|
|||||||
<transmittable-thread-local.version>2.14.4</transmittable-thread-local.version>
|
<transmittable-thread-local.version>2.14.4</transmittable-thread-local.version>
|
||||||
<hutool.version>5.8.33</hutool.version>
|
<hutool.version>5.8.33</hutool.version>
|
||||||
<lombok.version>1.18.36</lombok.version>
|
<lombok.version>1.18.36</lombok.version>
|
||||||
|
<mail.version>1.6.2</mail.version>
|
||||||
|
|
||||||
<!-- override dependency version -->
|
<!-- override dependency version -->
|
||||||
<tomcat.version>9.0.96</tomcat.version>
|
<tomcat.version>9.0.96</tomcat.version>
|
||||||
@@ -275,6 +276,20 @@
|
|||||||
<version>${wfc.version}</version>
|
<version>${wfc.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- 邮箱模块 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wfc</groupId>
|
||||||
|
<artifactId>wfc-common-mail</artifactId>
|
||||||
|
<version>${wfc.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- 邮箱 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.sun.mail</groupId>
|
||||||
|
<artifactId>javax.mail</artifactId>
|
||||||
|
<version>${mail.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</dependencyManagement>
|
</dependencyManagement>
|
||||||
|
|
||||||
|
|||||||
@@ -18,6 +18,7 @@
|
|||||||
<module>wfc-common-sensitive</module>
|
<module>wfc-common-sensitive</module>
|
||||||
<module>wfc-common-datascope</module>
|
<module>wfc-common-datascope</module>
|
||||||
<module>wfc-common-datasource</module>
|
<module>wfc-common-datasource</module>
|
||||||
|
<module>wfc-common-mail</module>
|
||||||
</modules>
|
</modules>
|
||||||
|
|
||||||
<artifactId>wfc-common</artifactId>
|
<artifactId>wfc-common</artifactId>
|
||||||
|
|||||||
@@ -0,0 +1,35 @@
|
|||||||
|
package org.wfc.common.core.constant;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 全局的key常量 (业务无关的key)
|
||||||
|
*
|
||||||
|
* @author cyc
|
||||||
|
*/
|
||||||
|
public interface GlobalConstants {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 全局 redis key (业务无关的key)
|
||||||
|
*/
|
||||||
|
String GLOBAL_REDIS_KEY = "global:";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 验证码 redis key
|
||||||
|
*/
|
||||||
|
String CAPTCHA_CODE_KEY = GLOBAL_REDIS_KEY + "captcha_codes:";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 防重提交 redis key
|
||||||
|
*/
|
||||||
|
String REPEAT_SUBMIT_KEY = GLOBAL_REDIS_KEY + "repeat_submit:";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 限流 redis key
|
||||||
|
*/
|
||||||
|
String RATE_LIMIT_KEY = GLOBAL_REDIS_KEY + "rate_limit:";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 三方认证 redis key
|
||||||
|
*/
|
||||||
|
String SOCIAL_AUTH_CODE_KEY = GLOBAL_REDIS_KEY + "social_auth_codes:";
|
||||||
|
|
||||||
|
}
|
||||||
@@ -18,6 +18,7 @@ user.username.length.valid=Account length must be between {min} and {max} charac
|
|||||||
user.password.not.blank=Username/Password cannot be empty
|
user.password.not.blank=Username/Password cannot be empty
|
||||||
user.password.length.valid=Password length must be between {min} and {max} characters
|
user.password.length.valid=Password length must be between {min} and {max} characters
|
||||||
user.password.not.valid=* 5-50 characters
|
user.password.not.valid=* 5-50 characters
|
||||||
|
user.email.not.enable=The email function is not enabled in the current system
|
||||||
user.email.not.valid=Mailbox format error
|
user.email.not.valid=Mailbox format error
|
||||||
user.email.not.blank=Mailbox cannot be blank
|
user.email.not.blank=Mailbox cannot be blank
|
||||||
user.phonenumber.not.blank=Phone number cannot be blank
|
user.phonenumber.not.blank=Phone number cannot be blank
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ user.username.length.valid=账户长度必须在{min}到{max}个字符之间
|
|||||||
user.password.not.blank=用户/密码不能为空
|
user.password.not.blank=用户/密码不能为空
|
||||||
user.password.length.valid=用户密码长度必须在{min}到{max}个字符之间
|
user.password.length.valid=用户密码长度必须在{min}到{max}个字符之间
|
||||||
user.password.not.valid=* 5-50个字符
|
user.password.not.valid=* 5-50个字符
|
||||||
|
user.email.not.enable=当前系统没有开启邮箱功能
|
||||||
user.email.not.valid=邮箱格式错误
|
user.email.not.valid=邮箱格式错误
|
||||||
user.email.not.blank=邮箱不能为空
|
user.email.not.blank=邮箱不能为空
|
||||||
user.phonenumber.not.blank=用户手机号不能为空
|
user.phonenumber.not.blank=用户手机号不能为空
|
||||||
|
|||||||
27
wfc-common/wfc-common-mail/pom.xml
Normal file
27
wfc-common/wfc-common-mail/pom.xml
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
<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>org.wfc</groupId>
|
||||||
|
<artifactId>wfc-common</artifactId>
|
||||||
|
<version>3.6.5</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<artifactId>wfc-common-mail</artifactId>
|
||||||
|
<description>
|
||||||
|
wfc-common-mail邮箱服务
|
||||||
|
</description>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wfc</groupId>
|
||||||
|
<artifactId>wfc-common-core</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.sun.mail</groupId>
|
||||||
|
<artifactId>javax.mail</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
package org.wfc.common.mail.config;
|
||||||
|
|
||||||
|
import cn.hutool.extra.mail.MailAccount;
|
||||||
|
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.wfc.common.mail.config.properties.MailProperties;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* JavaMail 配置
|
||||||
|
*
|
||||||
|
* @author Michelle.Chung
|
||||||
|
*/
|
||||||
|
@AutoConfiguration
|
||||||
|
@EnableConfigurationProperties(MailProperties.class)
|
||||||
|
public class MailConfiguration {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@ConditionalOnProperty(value = "mail.enabled", havingValue = "true")
|
||||||
|
public MailAccount mailAccount(MailProperties mailProperties) {
|
||||||
|
MailAccount account = new MailAccount();
|
||||||
|
account.setHost(mailProperties.getHost());
|
||||||
|
account.setPort(mailProperties.getPort());
|
||||||
|
account.setAuth(mailProperties.getAuth());
|
||||||
|
account.setFrom(mailProperties.getFrom());
|
||||||
|
account.setUser(mailProperties.getUser());
|
||||||
|
account.setPass(mailProperties.getPass());
|
||||||
|
account.setSocketFactoryPort(mailProperties.getPort());
|
||||||
|
account.setStarttlsEnable(mailProperties.getStarttlsEnable());
|
||||||
|
account.setSslEnable(mailProperties.getSslEnable());
|
||||||
|
account.setTimeout(mailProperties.getTimeout());
|
||||||
|
account.setConnectionTimeout(mailProperties.getConnectionTimeout());
|
||||||
|
return account;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,69 @@
|
|||||||
|
package org.wfc.common.mail.config.properties;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* JavaMail 配置属性
|
||||||
|
*
|
||||||
|
* @author caiyuchao
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@ConfigurationProperties(prefix = "mail")
|
||||||
|
public class MailProperties {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 过滤开关
|
||||||
|
*/
|
||||||
|
private Boolean enabled;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* SMTP服务器域名
|
||||||
|
*/
|
||||||
|
private String host;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* SMTP服务端口
|
||||||
|
*/
|
||||||
|
private Integer port;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否需要用户名密码验证
|
||||||
|
*/
|
||||||
|
private Boolean auth;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户名
|
||||||
|
*/
|
||||||
|
private String user;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 密码
|
||||||
|
*/
|
||||||
|
private String pass;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送方,遵循RFC-822标准
|
||||||
|
*/
|
||||||
|
private String from;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 使用 STARTTLS安全连接,STARTTLS是对纯文本通信协议的扩展。它将纯文本连接升级为加密连接(TLS或SSL), 而不是使用一个单独的加密通信端口。
|
||||||
|
*/
|
||||||
|
private Boolean starttlsEnable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 使用 SSL安全连接
|
||||||
|
*/
|
||||||
|
private Boolean sslEnable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* SMTP超时时长,单位毫秒,缺省值不超时
|
||||||
|
*/
|
||||||
|
private Long timeout;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Socket连接超时值,单位毫秒,缺省值不超时
|
||||||
|
*/
|
||||||
|
private Long connectionTimeout;
|
||||||
|
}
|
||||||
@@ -0,0 +1,468 @@
|
|||||||
|
package org.wfc.common.mail.utils;
|
||||||
|
|
||||||
|
import cn.hutool.core.collection.CollUtil;
|
||||||
|
import cn.hutool.core.io.IoUtil;
|
||||||
|
import cn.hutool.core.map.MapUtil;
|
||||||
|
import cn.hutool.core.util.CharUtil;
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import cn.hutool.extra.mail.Mail;
|
||||||
|
import cn.hutool.extra.mail.MailAccount;
|
||||||
|
import cn.hutool.extra.mail.UserPassAuthenticator;
|
||||||
|
import lombok.AccessLevel;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import org.wfc.common.core.utils.SpringUtils;
|
||||||
|
|
||||||
|
import javax.mail.Authenticator;
|
||||||
|
import javax.mail.Session;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Map.Entry;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 邮件工具类
|
||||||
|
*/
|
||||||
|
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||||
|
public class MailUtils {
|
||||||
|
|
||||||
|
private static final MailAccount ACCOUNT = SpringUtils.getBean(MailAccount.class);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取邮件发送实例
|
||||||
|
*/
|
||||||
|
public static MailAccount getMailAccount() {
|
||||||
|
return ACCOUNT;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取邮件发送实例 (自定义发送人以及授权码)
|
||||||
|
*
|
||||||
|
* @param user 发送人
|
||||||
|
* @param pass 授权码
|
||||||
|
*/
|
||||||
|
public static MailAccount getMailAccount(String from, String user, String pass) {
|
||||||
|
ACCOUNT.setFrom(StrUtil.blankToDefault(from, ACCOUNT.getFrom()));
|
||||||
|
ACCOUNT.setUser(StrUtil.blankToDefault(user, ACCOUNT.getUser()));
|
||||||
|
ACCOUNT.setPass(StrUtil.blankToDefault(pass, ACCOUNT.getPass()));
|
||||||
|
return ACCOUNT;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 使用配置文件中设置的账户发送文本邮件,发送给单个或多个收件人<br>
|
||||||
|
* 多个收件人可以使用逗号“,”分隔,也可以通过分号“;”分隔
|
||||||
|
*
|
||||||
|
* @param to 收件人
|
||||||
|
* @param subject 标题
|
||||||
|
* @param content 正文
|
||||||
|
* @param files 附件列表
|
||||||
|
* @return message-id
|
||||||
|
* @since 3.2.0
|
||||||
|
*/
|
||||||
|
public static String sendText(String to, String subject, String content, File... files) {
|
||||||
|
return send(to, subject, content, false, files);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 使用配置文件中设置的账户发送HTML邮件,发送给单个或多个收件人<br>
|
||||||
|
* 多个收件人可以使用逗号“,”分隔,也可以通过分号“;”分隔
|
||||||
|
*
|
||||||
|
* @param to 收件人
|
||||||
|
* @param subject 标题
|
||||||
|
* @param content 正文
|
||||||
|
* @param files 附件列表
|
||||||
|
* @return message-id
|
||||||
|
* @since 3.2.0
|
||||||
|
*/
|
||||||
|
public static String sendHtml(String to, String subject, String content, File... files) {
|
||||||
|
return send(to, subject, content, true, files);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 使用配置文件中设置的账户发送邮件,发送单个或多个收件人<br>
|
||||||
|
* 多个收件人可以使用逗号“,”分隔,也可以通过分号“;”分隔
|
||||||
|
*
|
||||||
|
* @param to 收件人
|
||||||
|
* @param subject 标题
|
||||||
|
* @param content 正文
|
||||||
|
* @param isHtml 是否为HTML
|
||||||
|
* @param files 附件列表
|
||||||
|
* @return message-id
|
||||||
|
*/
|
||||||
|
public static String send(String to, String subject, String content, boolean isHtml, File... files) {
|
||||||
|
return send(splitAddress(to), subject, content, isHtml, files);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 使用配置文件中设置的账户发送邮件,发送单个或多个收件人<br>
|
||||||
|
* 多个收件人、抄送人、密送人可以使用逗号“,”分隔,也可以通过分号“;”分隔
|
||||||
|
*
|
||||||
|
* @param to 收件人,可以使用逗号“,”分隔,也可以通过分号“;”分隔
|
||||||
|
* @param cc 抄送人,可以使用逗号“,”分隔,也可以通过分号“;”分隔
|
||||||
|
* @param bcc 密送人,可以使用逗号“,”分隔,也可以通过分号“;”分隔
|
||||||
|
* @param subject 标题
|
||||||
|
* @param content 正文
|
||||||
|
* @param isHtml 是否为HTML
|
||||||
|
* @param files 附件列表
|
||||||
|
* @return message-id
|
||||||
|
* @since 4.0.3
|
||||||
|
*/
|
||||||
|
public static String send(String to, String cc, String bcc, String subject, String content, boolean isHtml, File... files) {
|
||||||
|
return send(splitAddress(to), splitAddress(cc), splitAddress(bcc), subject, content, isHtml, files);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 使用配置文件中设置的账户发送文本邮件,发送给多人
|
||||||
|
*
|
||||||
|
* @param tos 收件人列表
|
||||||
|
* @param subject 标题
|
||||||
|
* @param content 正文
|
||||||
|
* @param files 附件列表
|
||||||
|
* @return message-id
|
||||||
|
*/
|
||||||
|
public static String sendText(Collection<String> tos, String subject, String content, File... files) {
|
||||||
|
return send(tos, subject, content, false, files);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 使用配置文件中设置的账户发送HTML邮件,发送给多人
|
||||||
|
*
|
||||||
|
* @param tos 收件人列表
|
||||||
|
* @param subject 标题
|
||||||
|
* @param content 正文
|
||||||
|
* @param files 附件列表
|
||||||
|
* @return message-id
|
||||||
|
* @since 3.2.0
|
||||||
|
*/
|
||||||
|
public static String sendHtml(Collection<String> tos, String subject, String content, File... files) {
|
||||||
|
return send(tos, subject, content, true, files);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 使用配置文件中设置的账户发送邮件,发送给多人
|
||||||
|
*
|
||||||
|
* @param tos 收件人列表
|
||||||
|
* @param subject 标题
|
||||||
|
* @param content 正文
|
||||||
|
* @param isHtml 是否为HTML
|
||||||
|
* @param files 附件列表
|
||||||
|
* @return message-id
|
||||||
|
*/
|
||||||
|
public static String send(Collection<String> tos, String subject, String content, boolean isHtml, File... files) {
|
||||||
|
return send(tos, null, null, subject, content, isHtml, files);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 使用配置文件中设置的账户发送邮件,发送给多人
|
||||||
|
*
|
||||||
|
* @param tos 收件人列表
|
||||||
|
* @param ccs 抄送人列表,可以为null或空
|
||||||
|
* @param bccs 密送人列表,可以为null或空
|
||||||
|
* @param subject 标题
|
||||||
|
* @param content 正文
|
||||||
|
* @param isHtml 是否为HTML
|
||||||
|
* @param files 附件列表
|
||||||
|
* @return message-id
|
||||||
|
* @since 4.0.3
|
||||||
|
*/
|
||||||
|
public static String send(Collection<String> tos, Collection<String> ccs, Collection<String> bccs, String subject, String content, boolean isHtml, File... files) {
|
||||||
|
return send(getMailAccount(), true, tos, ccs, bccs, subject, content, null, isHtml, files);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ------------------------------------------------------------------------------------------------------------------------------- Custom MailAccount
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送邮件给多人
|
||||||
|
*
|
||||||
|
* @param mailAccount 邮件认证对象
|
||||||
|
* @param to 收件人,多个收件人逗号或者分号隔开
|
||||||
|
* @param subject 标题
|
||||||
|
* @param content 正文
|
||||||
|
* @param isHtml 是否为HTML格式
|
||||||
|
* @param files 附件列表
|
||||||
|
* @return message-id
|
||||||
|
* @since 3.2.0
|
||||||
|
*/
|
||||||
|
public static String send(MailAccount mailAccount, String to, String subject, String content, boolean isHtml, File... files) {
|
||||||
|
return send(mailAccount, splitAddress(to), subject, content, isHtml, files);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送邮件给多人
|
||||||
|
*
|
||||||
|
* @param mailAccount 邮件帐户信息
|
||||||
|
* @param tos 收件人列表
|
||||||
|
* @param subject 标题
|
||||||
|
* @param content 正文
|
||||||
|
* @param isHtml 是否为HTML格式
|
||||||
|
* @param files 附件列表
|
||||||
|
* @return message-id
|
||||||
|
*/
|
||||||
|
public static String send(MailAccount mailAccount, Collection<String> tos, String subject, String content, boolean isHtml, File... files) {
|
||||||
|
return send(mailAccount, tos, null, null, subject, content, isHtml, files);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送邮件给多人
|
||||||
|
*
|
||||||
|
* @param mailAccount 邮件帐户信息
|
||||||
|
* @param tos 收件人列表
|
||||||
|
* @param ccs 抄送人列表,可以为null或空
|
||||||
|
* @param bccs 密送人列表,可以为null或空
|
||||||
|
* @param subject 标题
|
||||||
|
* @param content 正文
|
||||||
|
* @param isHtml 是否为HTML格式
|
||||||
|
* @param files 附件列表
|
||||||
|
* @return message-id
|
||||||
|
* @since 4.0.3
|
||||||
|
*/
|
||||||
|
public static String send(MailAccount mailAccount, Collection<String> tos, Collection<String> ccs, Collection<String> bccs, String subject, String content, boolean isHtml, File... files) {
|
||||||
|
return send(mailAccount, false, tos, ccs, bccs, subject, content, null, isHtml, files);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 使用配置文件中设置的账户发送HTML邮件,发送给单个或多个收件人<br>
|
||||||
|
* 多个收件人可以使用逗号“,”分隔,也可以通过分号“;”分隔
|
||||||
|
*
|
||||||
|
* @param to 收件人
|
||||||
|
* @param subject 标题
|
||||||
|
* @param content 正文
|
||||||
|
* @param imageMap 图片与占位符,占位符格式为cid:$IMAGE_PLACEHOLDER
|
||||||
|
* @param files 附件列表
|
||||||
|
* @return message-id
|
||||||
|
* @since 3.2.0
|
||||||
|
*/
|
||||||
|
public static String sendHtml(String to, String subject, String content, Map<String, InputStream> imageMap, File... files) {
|
||||||
|
return send(to, subject, content, imageMap, true, files);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 使用配置文件中设置的账户发送邮件,发送单个或多个收件人<br>
|
||||||
|
* 多个收件人可以使用逗号“,”分隔,也可以通过分号“;”分隔
|
||||||
|
*
|
||||||
|
* @param to 收件人
|
||||||
|
* @param subject 标题
|
||||||
|
* @param content 正文
|
||||||
|
* @param imageMap 图片与占位符,占位符格式为cid:$IMAGE_PLACEHOLDER
|
||||||
|
* @param isHtml 是否为HTML
|
||||||
|
* @param files 附件列表
|
||||||
|
* @return message-id
|
||||||
|
*/
|
||||||
|
public static String send(String to, String subject, String content, Map<String, InputStream> imageMap, boolean isHtml, File... files) {
|
||||||
|
return send(splitAddress(to), subject, content, imageMap, isHtml, files);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 使用配置文件中设置的账户发送邮件,发送单个或多个收件人<br>
|
||||||
|
* 多个收件人、抄送人、密送人可以使用逗号“,”分隔,也可以通过分号“;”分隔
|
||||||
|
*
|
||||||
|
* @param to 收件人,可以使用逗号“,”分隔,也可以通过分号“;”分隔
|
||||||
|
* @param cc 抄送人,可以使用逗号“,”分隔,也可以通过分号“;”分隔
|
||||||
|
* @param bcc 密送人,可以使用逗号“,”分隔,也可以通过分号“;”分隔
|
||||||
|
* @param subject 标题
|
||||||
|
* @param content 正文
|
||||||
|
* @param imageMap 图片与占位符,占位符格式为cid:$IMAGE_PLACEHOLDER
|
||||||
|
* @param isHtml 是否为HTML
|
||||||
|
* @param files 附件列表
|
||||||
|
* @return message-id
|
||||||
|
* @since 4.0.3
|
||||||
|
*/
|
||||||
|
public static String send(String to, String cc, String bcc, String subject, String content, Map<String, InputStream> imageMap, boolean isHtml, File... files) {
|
||||||
|
return send(splitAddress(to), splitAddress(cc), splitAddress(bcc), subject, content, imageMap, isHtml, files);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 使用配置文件中设置的账户发送HTML邮件,发送给多人
|
||||||
|
*
|
||||||
|
* @param tos 收件人列表
|
||||||
|
* @param subject 标题
|
||||||
|
* @param content 正文
|
||||||
|
* @param imageMap 图片与占位符,占位符格式为cid:$IMAGE_PLACEHOLDER
|
||||||
|
* @param files 附件列表
|
||||||
|
* @return message-id
|
||||||
|
* @since 3.2.0
|
||||||
|
*/
|
||||||
|
public static String sendHtml(Collection<String> tos, String subject, String content, Map<String, InputStream> imageMap, File... files) {
|
||||||
|
return send(tos, subject, content, imageMap, true, files);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 使用配置文件中设置的账户发送邮件,发送给多人
|
||||||
|
*
|
||||||
|
* @param tos 收件人列表
|
||||||
|
* @param subject 标题
|
||||||
|
* @param content 正文
|
||||||
|
* @param imageMap 图片与占位符,占位符格式为cid:$IMAGE_PLACEHOLDER
|
||||||
|
* @param isHtml 是否为HTML
|
||||||
|
* @param files 附件列表
|
||||||
|
* @return message-id
|
||||||
|
*/
|
||||||
|
public static String send(Collection<String> tos, String subject, String content, Map<String, InputStream> imageMap, boolean isHtml, File... files) {
|
||||||
|
return send(tos, null, null, subject, content, imageMap, isHtml, files);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 使用配置文件中设置的账户发送邮件,发送给多人
|
||||||
|
*
|
||||||
|
* @param tos 收件人列表
|
||||||
|
* @param ccs 抄送人列表,可以为null或空
|
||||||
|
* @param bccs 密送人列表,可以为null或空
|
||||||
|
* @param subject 标题
|
||||||
|
* @param content 正文
|
||||||
|
* @param imageMap 图片与占位符,占位符格式为cid:$IMAGE_PLACEHOLDER
|
||||||
|
* @param isHtml 是否为HTML
|
||||||
|
* @param files 附件列表
|
||||||
|
* @return message-id
|
||||||
|
* @since 4.0.3
|
||||||
|
*/
|
||||||
|
public static String send(Collection<String> tos, Collection<String> ccs, Collection<String> bccs, String subject, String content, Map<String, InputStream> imageMap, boolean isHtml, File... files) {
|
||||||
|
return send(getMailAccount(), true, tos, ccs, bccs, subject, content, imageMap, isHtml, files);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ------------------------------------------------------------------------------------------------------------------------------- Custom MailAccount
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送邮件给多人
|
||||||
|
*
|
||||||
|
* @param mailAccount 邮件认证对象
|
||||||
|
* @param to 收件人,多个收件人逗号或者分号隔开
|
||||||
|
* @param subject 标题
|
||||||
|
* @param content 正文
|
||||||
|
* @param imageMap 图片与占位符,占位符格式为cid:$IMAGE_PLACEHOLDER
|
||||||
|
* @param isHtml 是否为HTML格式
|
||||||
|
* @param files 附件列表
|
||||||
|
* @return message-id
|
||||||
|
* @since 3.2.0
|
||||||
|
*/
|
||||||
|
public static String send(MailAccount mailAccount, String to, String subject, String content, Map<String, InputStream> imageMap, boolean isHtml, File... files) {
|
||||||
|
return send(mailAccount, splitAddress(to), subject, content, imageMap, isHtml, files);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送邮件给多人
|
||||||
|
*
|
||||||
|
* @param mailAccount 邮件帐户信息
|
||||||
|
* @param tos 收件人列表
|
||||||
|
* @param subject 标题
|
||||||
|
* @param content 正文
|
||||||
|
* @param imageMap 图片与占位符,占位符格式为cid:$IMAGE_PLACEHOLDER
|
||||||
|
* @param isHtml 是否为HTML格式
|
||||||
|
* @param files 附件列表
|
||||||
|
* @return message-id
|
||||||
|
* @since 4.6.3
|
||||||
|
*/
|
||||||
|
public static String send(MailAccount mailAccount, Collection<String> tos, String subject, String content, Map<String, InputStream> imageMap, boolean isHtml, File... files) {
|
||||||
|
return send(mailAccount, tos, null, null, subject, content, imageMap, isHtml, files);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送邮件给多人
|
||||||
|
*
|
||||||
|
* @param mailAccount 邮件帐户信息
|
||||||
|
* @param tos 收件人列表
|
||||||
|
* @param ccs 抄送人列表,可以为null或空
|
||||||
|
* @param bccs 密送人列表,可以为null或空
|
||||||
|
* @param subject 标题
|
||||||
|
* @param content 正文
|
||||||
|
* @param imageMap 图片与占位符,占位符格式为cid:$IMAGE_PLACEHOLDER
|
||||||
|
* @param isHtml 是否为HTML格式
|
||||||
|
* @param files 附件列表
|
||||||
|
* @return message-id
|
||||||
|
* @since 4.6.3
|
||||||
|
*/
|
||||||
|
public static String send(MailAccount mailAccount, Collection<String> tos, Collection<String> ccs, Collection<String> bccs, String subject, String content, Map<String, InputStream> imageMap,
|
||||||
|
boolean isHtml, File... files) {
|
||||||
|
return send(mailAccount, false, tos, ccs, bccs, subject, content, imageMap, isHtml, files);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据配置文件,获取邮件客户端会话
|
||||||
|
*
|
||||||
|
* @param mailAccount 邮件账户配置
|
||||||
|
* @param isSingleton 是否单例(全局共享会话)
|
||||||
|
* @return {@link Session}
|
||||||
|
* @since 5.5.7
|
||||||
|
*/
|
||||||
|
public static Session getSession(MailAccount mailAccount, boolean isSingleton) {
|
||||||
|
Authenticator authenticator = null;
|
||||||
|
if (mailAccount.isAuth()) {
|
||||||
|
authenticator = new UserPassAuthenticator(mailAccount.getUser(), mailAccount.getPass());
|
||||||
|
}
|
||||||
|
|
||||||
|
return isSingleton ? Session.getDefaultInstance(mailAccount.getSmtpProps(), authenticator) //
|
||||||
|
: Session.getInstance(mailAccount.getSmtpProps(), authenticator);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ------------------------------------------------------------------------------------------------------------------------ Private method start
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送邮件给多人
|
||||||
|
*
|
||||||
|
* @param mailAccount 邮件帐户信息
|
||||||
|
* @param useGlobalSession 是否全局共享Session
|
||||||
|
* @param tos 收件人列表
|
||||||
|
* @param ccs 抄送人列表,可以为null或空
|
||||||
|
* @param bccs 密送人列表,可以为null或空
|
||||||
|
* @param subject 标题
|
||||||
|
* @param content 正文
|
||||||
|
* @param imageMap 图片与占位符,占位符格式为cid:${cid}
|
||||||
|
* @param isHtml 是否为HTML格式
|
||||||
|
* @param files 附件列表
|
||||||
|
* @return message-id
|
||||||
|
* @since 4.6.3
|
||||||
|
*/
|
||||||
|
private static String send(MailAccount mailAccount, boolean useGlobalSession, Collection<String> tos, Collection<String> ccs, Collection<String> bccs, String subject, String content,
|
||||||
|
Map<String, InputStream> imageMap, boolean isHtml, File... files) {
|
||||||
|
final Mail mail = Mail.create(mailAccount).setUseGlobalSession(useGlobalSession);
|
||||||
|
|
||||||
|
// 可选抄送人
|
||||||
|
if (CollUtil.isNotEmpty(ccs)) {
|
||||||
|
mail.setCcs(ccs.toArray(new String[0]));
|
||||||
|
}
|
||||||
|
// 可选密送人
|
||||||
|
if (CollUtil.isNotEmpty(bccs)) {
|
||||||
|
mail.setBccs(bccs.toArray(new String[0]));
|
||||||
|
}
|
||||||
|
|
||||||
|
mail.setTos(tos.toArray(new String[0]));
|
||||||
|
mail.setTitle(subject);
|
||||||
|
mail.setContent(content);
|
||||||
|
mail.setHtml(isHtml);
|
||||||
|
mail.setFiles(files);
|
||||||
|
|
||||||
|
// 图片
|
||||||
|
if (MapUtil.isNotEmpty(imageMap)) {
|
||||||
|
for (Entry<String, InputStream> entry : imageMap.entrySet()) {
|
||||||
|
mail.addImage(entry.getKey(), entry.getValue());
|
||||||
|
// 关闭流
|
||||||
|
IoUtil.close(entry.getValue());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return mail.send();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将多个联系人转为列表,分隔符为逗号或者分号
|
||||||
|
*
|
||||||
|
* @param addresses 多个联系人,如果为空返回null
|
||||||
|
* @return 联系人列表
|
||||||
|
*/
|
||||||
|
private static List<String> splitAddress(String addresses) {
|
||||||
|
if (StrUtil.isBlank(addresses)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
List<String> result;
|
||||||
|
if (StrUtil.contains(addresses, CharUtil.COMMA)) {
|
||||||
|
result = StrUtil.splitTrim(addresses, CharUtil.COMMA);
|
||||||
|
} else if (StrUtil.contains(addresses, ';')) {
|
||||||
|
result = StrUtil.splitTrim(addresses, ';');
|
||||||
|
} else {
|
||||||
|
result = CollUtil.newArrayList(addresses);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
// ------------------------------------------------------------------------------------------------------------------------ Private method end
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
org.wfc.common.mail.config.MailConfiguration
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
mail:
|
||||||
|
enabled: false
|
||||||
|
host: smtp.163.com
|
||||||
|
port: 465
|
||||||
|
# 是否需要用户名密码验证
|
||||||
|
auth: true
|
||||||
|
# 发送方,遵循RFC-822标准
|
||||||
|
from: xxx@163.com
|
||||||
|
# 用户名(注意:如果使用foxmail邮箱,此处user为qq号)
|
||||||
|
user: xxx@163.com
|
||||||
|
# 密码(注意,某些邮箱需要为SMTP服务单独设置密码,详情查看相关帮助)
|
||||||
|
pass: xxxxxxxxxx
|
||||||
|
# 使用 STARTTLS安全连接,STARTTLS是对纯文本通信协议的扩展。
|
||||||
|
starttlsEnable: true
|
||||||
|
# 使用SSL安全连接
|
||||||
|
sslEnable: true
|
||||||
|
# SMTP超时时长,单位毫秒,缺省值不超时
|
||||||
|
timeout: 0
|
||||||
|
# Socket连接超时值,单位毫秒,缺省值不超时
|
||||||
|
connectionTimeout: 0
|
||||||
@@ -71,6 +71,11 @@
|
|||||||
<artifactId>wfc-common-swagger</artifactId>
|
<artifactId>wfc-common-swagger</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wfc</groupId>
|
||||||
|
<artifactId>wfc-common-mail</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
@@ -0,0 +1,60 @@
|
|||||||
|
package org.wfc.system.controller;
|
||||||
|
|
||||||
|
|
||||||
|
import cn.hutool.core.util.RandomUtil;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.validation.annotation.Validated;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
import org.wfc.common.core.constant.Constants;
|
||||||
|
import org.wfc.common.core.constant.GlobalConstants;
|
||||||
|
import org.wfc.common.core.domain.R;
|
||||||
|
import org.wfc.common.core.utils.MessageUtils;
|
||||||
|
import org.wfc.common.core.web.controller.BaseController;
|
||||||
|
import org.wfc.common.mail.config.properties.MailProperties;
|
||||||
|
import org.wfc.common.mail.utils.MailUtils;
|
||||||
|
import org.wfc.common.redis.service.RedisService;
|
||||||
|
|
||||||
|
import javax.validation.constraints.NotBlank;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 邮件功能
|
||||||
|
*
|
||||||
|
* @author cyc
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@Validated
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/email")
|
||||||
|
public class SysEmailController extends BaseController {
|
||||||
|
|
||||||
|
private final MailProperties mailProperties;
|
||||||
|
private final RedisService redisService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 邮箱验证码
|
||||||
|
*
|
||||||
|
* @param email 邮箱
|
||||||
|
*/
|
||||||
|
@GetMapping("/code")
|
||||||
|
public R<Void> emailCode(@NotBlank(message = "{user.email.not.blank}") String email) {
|
||||||
|
if (!mailProperties.getEnabled()) {
|
||||||
|
return R.fail(MessageUtils.message("user.email.not.enable"));
|
||||||
|
}
|
||||||
|
String key = GlobalConstants.CAPTCHA_CODE_KEY + email;
|
||||||
|
String code = RandomUtil.randomNumbers(4);
|
||||||
|
redisService.setCacheObject(key, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
|
||||||
|
try {
|
||||||
|
MailUtils.sendText(email, "登录验证码", "您本次验证码为:" + code + ",有效性为" + Constants.CAPTCHA_EXPIRATION + "分钟,请尽快填写。");
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("验证码短信发送异常 => {}", e.getMessage());
|
||||||
|
return R.fail(e.getMessage());
|
||||||
|
}
|
||||||
|
return R.ok();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user