[add]:
1. (common) pom.xml - 增加邮件依赖 ; 2. (admin) application-dev.yml - 增加邮件配置 ; 3. (framework) MailProperties, MailConfig - 增加邮件属性配置 ; 4. (common) MailUtils - 重写 Hutool MailUtil方法 ; 5. (demo) MailController - 邮件发送测试方法 ;
This commit is contained in:
parent
e18cf51c01
commit
92f030887b
1
pom.xml
1
pom.xml
@ -38,6 +38,7 @@
|
||||
<dynamic-ds.version>3.5.1</dynamic-ds.version>
|
||||
<tlog.version>1.3.6</tlog.version>
|
||||
<xxl-job.version>2.3.0</xxl-job.version>
|
||||
<mail.version>1.6.2</mail.version>
|
||||
|
||||
<!-- jdk11 缺失依赖 jaxb-->
|
||||
<jaxb.version>3.0.1</jaxb.version>
|
||||
|
@ -137,6 +137,34 @@ spring:
|
||||
# 是否开启ssl
|
||||
ssl: false
|
||||
|
||||
# 邮件
|
||||
mail:
|
||||
enabled: false
|
||||
# 邮件服务地址 (enabled = true 时打开该配置)
|
||||
# host: smtp.qq.com
|
||||
# 用户名
|
||||
username: xxx@qq.com
|
||||
# 授权码 (设置 - 账户 - POP3/SMTP服务)
|
||||
password: xxx
|
||||
# QQ邮箱加密端口,不同邮箱的端口不一样
|
||||
port: 465
|
||||
properties:
|
||||
mail:
|
||||
smtp:
|
||||
socketFactory:
|
||||
class: javax.net.ssl.SSLSocketFactory
|
||||
ssl:
|
||||
trust: smtp.qq.com
|
||||
# 是否需要用户认证
|
||||
auth: true
|
||||
starttls:
|
||||
# 启用TLS加密
|
||||
enable: true
|
||||
required: true
|
||||
# 传输协议 starttls.enable = true 时为 smtps
|
||||
protocol: smtps
|
||||
debug: true
|
||||
|
||||
redisson:
|
||||
# 线程池数量
|
||||
threads: 4
|
||||
|
@ -159,6 +159,12 @@
|
||||
<artifactId>lock4j-redisson-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.sun.mail</groupId>
|
||||
<artifactId>javax.mail</artifactId>
|
||||
<version>${mail.version}</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
@ -0,0 +1,468 @@
|
||||
package com.ruoyi.common.utils.email;
|
||||
|
||||
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.*;
|
||||
import com.ruoyi.common.utils.StringUtils;
|
||||
import com.ruoyi.common.utils.spring.SpringUtils;
|
||||
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 username 发送人
|
||||
* @param password 授权码
|
||||
*/
|
||||
public static MailAccount getMailAccount(String username, String password) {
|
||||
ACCOUNT.setFrom(StringUtils.blankToDefault(username, ACCOUNT.getUser()));
|
||||
ACCOUNT.setUser(StringUtils.blankToDefault(username, ACCOUNT.getUser()));
|
||||
ACCOUNT.setPass(StringUtils.blankToDefault(password, 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,54 @@
|
||||
package com.ruoyi.demo.controller;
|
||||
|
||||
import com.ruoyi.common.core.domain.R;
|
||||
import com.ruoyi.common.utils.email.MailUtils;
|
||||
import io.swagger.annotations.Api;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
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 java.io.File;
|
||||
|
||||
|
||||
/**
|
||||
* 测试邮件发送 Controller
|
||||
*
|
||||
* @author Michelle.Chung
|
||||
*/
|
||||
@Validated
|
||||
@Api(value = "邮件控制器", tags = {"测试邮件发送"})
|
||||
@RequiredArgsConstructor(onConstructor_ = @Autowired)
|
||||
@RequestMapping("/demo/mail")
|
||||
@RestController
|
||||
public class MailController {
|
||||
|
||||
/**
|
||||
* 发送邮件
|
||||
*
|
||||
* @param to 接收人
|
||||
* @param subject 标题
|
||||
* @param text 内容
|
||||
*/
|
||||
@GetMapping("/sendSimpleMessage")
|
||||
public R<Void> sendSimpleMessage(String to, String subject, String text) {
|
||||
MailUtils.send(to, subject, text, false);
|
||||
return R.ok("操作成功");
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送邮件(带附件)
|
||||
*
|
||||
* @param to 接收人
|
||||
* @param subject 标题
|
||||
* @param text 内容
|
||||
*/
|
||||
@GetMapping("/sendMessageWithAttachment")
|
||||
public R<Void> sendMessageWithAttachment(String to, String subject, String text, String filePath) {
|
||||
MailUtils.send(to, subject, text, false, new File(filePath));
|
||||
return R.ok("操作成功");
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
package com.ruoyi.framework.config;
|
||||
|
||||
import cn.hutool.extra.mail.MailAccount;
|
||||
import com.ruoyi.framework.config.properties.MailProperties;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* JavaMail 配置
|
||||
*
|
||||
* @author Michelle.Chung
|
||||
*/
|
||||
@Configuration
|
||||
public class MailConfig {
|
||||
|
||||
@Resource
|
||||
private MailProperties mailProperties;
|
||||
|
||||
/**
|
||||
* 初始化 JavaMailSender
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnProperty(value = "spring.mail.enabled", havingValue = "true")
|
||||
public MailAccount mailAccount() {
|
||||
MailAccount account = new MailAccount();
|
||||
account.setFrom(mailProperties.getUsername());
|
||||
account.setUser(mailProperties.getUsername());
|
||||
account.setPass(mailProperties.getPassword());
|
||||
account.setPort(mailProperties.getPort());
|
||||
account.setAuth(mailProperties.getAuth());
|
||||
account.setDebug(mailProperties.getDebug());
|
||||
account.setStarttlsEnable(mailProperties.getStarttlsEnable());
|
||||
return account;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,65 @@
|
||||
package com.ruoyi.framework.config.properties;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* JavaMail 配置属性
|
||||
*
|
||||
* @author Michelle.Chung
|
||||
*/
|
||||
@Data
|
||||
@Component
|
||||
@ConfigurationProperties(prefix = "spring.mail")
|
||||
public class MailProperties {
|
||||
|
||||
/**
|
||||
* 过滤开关
|
||||
*/
|
||||
private String enabled;
|
||||
|
||||
/**
|
||||
* 邮件服务地址
|
||||
*/
|
||||
private String host;
|
||||
|
||||
/**
|
||||
* 用户名
|
||||
*/
|
||||
private String username;
|
||||
|
||||
/**
|
||||
* 授权码 (设置 - 账户 - POP3/SMTP服务)
|
||||
*/
|
||||
private String password;
|
||||
|
||||
/**
|
||||
* 邮箱加密端口,不同邮箱的端口不一样
|
||||
*/
|
||||
private Integer port;
|
||||
|
||||
/**
|
||||
* 是否需要用户认证
|
||||
*/
|
||||
@Value("${spring.mail.properties.mail.smtp.auth}")
|
||||
private Boolean auth;
|
||||
|
||||
/**
|
||||
* 是否启用TLS加密
|
||||
*/
|
||||
@Value("${spring.mail.properties.mail.smtp.starttls.enable}")
|
||||
private Boolean starttlsEnable;
|
||||
|
||||
@Value("${spring.mail.properties.mail.smtp.ssl.trust}")
|
||||
private String sslTrust;
|
||||
|
||||
private Boolean debug;
|
||||
|
||||
/**
|
||||
* 传输协议 starttls.enable = true 时为 smtps
|
||||
*/
|
||||
private String protocol;
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user