mirror of
https://gitee.com/dromara/RuoYi-Cloud-Plus.git
synced 2025-11-28 01:00:05 +08:00
add 增加 ruoyi-common-mail 邮件模块
This commit is contained in:
@@ -292,3 +292,24 @@ seata:
|
|||||||
server-addr: ${spring.cloud.nacos.server-addr}
|
server-addr: ${spring.cloud.nacos.server-addr}
|
||||||
group: ${spring.cloud.nacos.discovery.group}
|
group: ${spring.cloud.nacos.discovery.group}
|
||||||
namespace: ${spring.profiles.active}
|
namespace: ${spring.profiles.active}
|
||||||
|
|
||||||
|
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
|
||||||
|
|||||||
@@ -27,6 +27,7 @@
|
|||||||
<module>ruoyi-common-loadbalancer</module>
|
<module>ruoyi-common-loadbalancer</module>
|
||||||
<module>ruoyi-common-oss</module>
|
<module>ruoyi-common-oss</module>
|
||||||
<module>ruoyi-common-idempotent</module>
|
<module>ruoyi-common-idempotent</module>
|
||||||
|
<module>ruoyi-common-mail</module>
|
||||||
</modules>
|
</modules>
|
||||||
|
|
||||||
<artifactId>ruoyi-common</artifactId>
|
<artifactId>ruoyi-common</artifactId>
|
||||||
|
|||||||
@@ -120,6 +120,12 @@
|
|||||||
<version>${project.version}</version>
|
<version>${project.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.ruoyi</groupId>
|
||||||
|
<artifactId>ruoyi-common-mail</artifactId>
|
||||||
|
<version>${project.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</dependencyManagement>
|
</dependencyManagement>
|
||||||
</project>
|
</project>
|
||||||
|
|||||||
31
ruoyi-common/ruoyi-common-mail/pom.xml
Normal file
31
ruoyi-common/ruoyi-common-mail/pom.xml
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<parent>
|
||||||
|
<groupId>com.ruoyi</groupId>
|
||||||
|
<artifactId>ruoyi-common</artifactId>
|
||||||
|
<version>0.10.0</version>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<artifactId>ruoyi-common-mail</artifactId>
|
||||||
|
|
||||||
|
<description>
|
||||||
|
ruoyi-common-mail 邮件模块
|
||||||
|
</description>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.ruoyi</groupId>
|
||||||
|
<artifactId>ruoyi-common-core</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.sun.mail</groupId>
|
||||||
|
<artifactId>jakarta.mail</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
package com.ruoyi.common.mail.config;
|
||||||
|
|
||||||
|
import cn.hutool.extra.mail.MailAccount;
|
||||||
|
import com.ruoyi.common.mail.config.properties.MailProperties;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* JavaMail 配置
|
||||||
|
*
|
||||||
|
* @author Michelle.Chung
|
||||||
|
*/
|
||||||
|
@ConditionalOnProperty(value = "mail.enabled", havingValue = "true")
|
||||||
|
@EnableConfigurationProperties(MailProperties.class)
|
||||||
|
public class MailConfiguration {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
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 com.ruoyi.common.mail.config.properties;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* JavaMail 配置属性
|
||||||
|
*
|
||||||
|
* @author Michelle.Chung
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@ConfigurationProperties(prefix = "mail")
|
||||||
|
public class MailProperties {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 过滤开关
|
||||||
|
*/
|
||||||
|
private String 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,470 @@
|
|||||||
|
package com.ruoyi.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 com.ruoyi.common.core.utils.SpringUtils;
|
||||||
|
import com.ruoyi.common.core.utils.StringUtils;
|
||||||
|
import lombok.AccessLevel;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 邮件工具类
|
||||||
|
*/
|
||||||
|
@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(StringUtils.blankToDefault(from, ACCOUNT.getFrom()));
|
||||||
|
ACCOUNT.setUser(StringUtils.blankToDefault(user, ACCOUNT.getUser()));
|
||||||
|
ACCOUNT.setPass(StringUtils.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 (Map.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,2 @@
|
|||||||
|
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||||
|
com.ruoyi.common.mail.config.MailConfiguration
|
||||||
@@ -95,6 +95,11 @@
|
|||||||
<artifactId>ruoyi-api-resource</artifactId>
|
<artifactId>ruoyi-api-resource</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.ruoyi</groupId>
|
||||||
|
<artifactId>ruoyi-common-mail</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
Reference in New Issue
Block a user