Commit 828901991da4e07abeb67051c372b9ca9ebb1d07

Authored by 芋道源码
Committed by Gitee
2 parents 9c224341 a1164f24

!1209 【功能优化】邮件发送支持抄送和密送

Merge pull request !1209 from jinmh/master-jdk17
sql/mysql/ruoyi-vue-pro.sql
... ... @@ -1259,14 +1259,16 @@ CREATE TABLE `system_mail_log` (
1259 1259 `id` bigint NOT NULL AUTO_INCREMENT COMMENT '编号',
1260 1260 `user_id` bigint NULL DEFAULT NULL COMMENT '用户编号',
1261 1261 `user_type` tinyint NULL DEFAULT NULL COMMENT '用户类型',
1262   - `to_mail` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '接收邮箱地址',
  1262 + `to_mail` varchar(1024) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '接收邮箱地址',
  1263 + `cc_mail` varchar(1024) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '抄送邮箱地址',
  1264 + `bcc_mail` varchar(1024) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '密送邮箱地址',
1263 1265 `account_id` bigint NOT NULL COMMENT '邮箱账号编号',
1264 1266 `from_mail` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '发送邮箱地址',
1265 1267 `template_id` bigint NOT NULL COMMENT '模板编号',
1266 1268 `template_code` varchar(63) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '模板编码',
1267 1269 `template_nickname` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT '模版发送人名称',
1268 1270 `template_title` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '邮件标题',
1269   - `template_content` varchar(10240) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '邮件内容',
  1271 + `template_content` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '邮件内容',
1270 1272 `template_params` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '邮件参数',
1271 1273 `send_status` tinyint NOT NULL DEFAULT 0 COMMENT '发送状态',
1272 1274 `send_time` datetime NULL DEFAULT NULL COMMENT '发送时间',
... ...
yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/controller/admin/mail/MailTemplateController.java
... ... @@ -91,7 +91,7 @@ public class MailTemplateController {
91 91 @Operation(summary = "发送短信")
92 92 @PreAuthorize("@ss.hasPermission('system:mail-template:send-mail')")
93 93 public CommonResult<Long> sendMail(@Valid @RequestBody MailTemplateSendReqVO sendReqVO) {
94   - return success(mailSendService.sendSingleMailToAdmin(sendReqVO.getMail(), getLoginUserId(),
  94 + return success(mailSendService.sendMultipleMailToAdmin(sendReqVO.getToMails(), sendReqVO.getCcMails(), sendReqVO.getBccMails(), getLoginUserId(),
95 95 sendReqVO.getTemplateCode(), sendReqVO.getTemplateParams()));
96 96 }
97 97  
... ...
yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/controller/admin/mail/vo/log/MailLogRespVO.java
... ... @@ -2,13 +2,10 @@ package cn.iocoder.yudao.module.system.controller.admin.mail.vo.log;
2 2  
3 3 import io.swagger.v3.oas.annotations.media.Schema;
4 4 import lombok.Data;
5   -import org.springframework.format.annotation.DateTimeFormat;
6 5  
7 6 import java.time.LocalDateTime;
8 7 import java.util.Map;
9 8  
10   -import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;
11   -
12 9 @Schema(description = "管理后台 - 邮件日志 Response VO")
13 10 @Data
14 11 public class MailLogRespVO {
... ... @@ -22,9 +19,15 @@ public class MailLogRespVO {
22 19 @Schema(description = "用户类型,参见 UserTypeEnum 枚举", example = "2")
23 20 private Byte userType;
24 21  
25   - @Schema(description = "接收邮箱地址", requiredMode = Schema.RequiredMode.REQUIRED, example = "76854@qq.com")
  22 + @Schema(description = "接收邮箱地址", requiredMode = Schema.RequiredMode.REQUIRED, example = "user1@example.com, user2@example.com")
26 23 private String toMail;
27 24  
  25 + @Schema(description = "抄送邮箱地址", requiredMode = Schema.RequiredMode.REQUIRED, example = "user3@example.com, user4@example.com")
  26 + private String ccMail;
  27 +
  28 + @Schema(description = "密送邮箱地址", requiredMode = Schema.RequiredMode.REQUIRED, example = "user5@example.com, user6@example.com")
  29 + private String bccMail;
  30 +
28 31 @Schema(description = "邮箱账号编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "18107")
29 32 private Long accountId;
30 33  
... ...
yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/controller/admin/mail/vo/template/MailTemplateSendReqVO.java
... ... @@ -5,15 +5,22 @@ import lombok.Data;
5 5  
6 6 import jakarta.validation.constraints.NotEmpty;
7 7 import jakarta.validation.constraints.NotNull;
  8 +import java.util.List;
8 9 import java.util.Map;
9 10  
10 11 @Schema(description = "管理后台 - 邮件发送 Req VO")
11 12 @Data
12 13 public class MailTemplateSendReqVO {
13 14  
14   - @Schema(description = "接收邮箱", requiredMode = Schema.RequiredMode.REQUIRED, example = "7685413@qq.com")
  15 + @Schema(description = "接收邮箱", requiredMode = Schema.RequiredMode.REQUIRED, example = "[user1@example.com, user2@example.com]")
15 16 @NotEmpty(message = "接收邮箱不能为空")
16   - private String mail;
  17 + private List<String> toMails;
  18 +
  19 + @Schema(description = "抄送邮箱", requiredMode = Schema.RequiredMode.REQUIRED, example = "[user3@example.com, user4@example.com]")
  20 + private List<String> ccMails;
  21 +
  22 + @Schema(description = "密送邮箱", requiredMode = Schema.RequiredMode.REQUIRED, example = "[user5@example.com, user6@example.com]")
  23 + private List<String> bccMails;
17 24  
18 25 @Schema(description = "模板编码", requiredMode = Schema.RequiredMode.REQUIRED, example = "test_01")
19 26 @NotNull(message = "模板编码不能为空")
... ...
yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/mail/MailLogDO.java
... ... @@ -51,7 +51,14 @@ public class MailLogDO extends BaseDO implements Serializable {
51 51 * 接收邮箱地址
52 52 */
53 53 private String toMail;
54   -
  54 + /**
  55 + * 接收邮箱地址
  56 + */
  57 + private String ccMail;
  58 + /**
  59 + * 接收邮箱地址
  60 + */
  61 + private String bccMail;
55 62 /**
56 63 * 邮箱账号编号
57 64 *
... ...
yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/mq/message/mail/MailSendMessage.java
... ... @@ -5,6 +5,8 @@ import lombok.Data;
5 5 import jakarta.validation.constraints.NotEmpty;
6 6 import jakarta.validation.constraints.NotNull;
7 7  
  8 +import java.util.List;
  9 +
8 10 /**
9 11 * 邮箱发送消息
10 12 *
... ... @@ -21,8 +23,16 @@ public class MailSendMessage {
21 23 /**
22 24 * 接收邮件地址
23 25 */
24   - @NotNull(message = "接收邮件地址不能为空")
25   - private String mail;
  26 + @NotEmpty(message = "接收邮件地址不能为空")
  27 + private List<String> toMails;
  28 + /**
  29 + * 抄送邮件地址
  30 + */
  31 + private List<String> ccMails;
  32 + /**
  33 + * 密送邮件地址
  34 + */
  35 + private List<String> bccMails;
26 36 /**
27 37 * 邮件账号编号
28 38 */
... ...
yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/mq/producer/mail/MailProducer.java
... ... @@ -7,6 +7,10 @@ import org.springframework.stereotype.Component;
7 7  
8 8 import jakarta.annotation.Resource;
9 9  
  10 +import java.util.List;
  11 +
  12 +import static java.util.Collections.singletonList;
  13 +
10 14 /**
11 15 * Mail 邮件相关消息的 Producer
12 16 *
... ... @@ -24,17 +28,36 @@ public class MailProducer {
24 28 * 发送 {@link MailSendMessage} 消息
25 29 *
26 30 * @param sendLogId 发送日志编码
27   - * @param mail 接收邮件地址
  31 + * @param mail 接收邮件地址
28 32 * @param accountId 邮件账号编号
29   - * @param nickname 邮件发件人
30   - * @param title 邮件标题
31   - * @param content 邮件内容
  33 + * @param nickname 邮件发件人
  34 + * @param title 邮件标题
  35 + * @param content 邮件内容
32 36 */
33 37 public void sendMailSendMessage(Long sendLogId, String mail, Long accountId,
34 38 String nickname, String title, String content) {
  39 + sendMailSendMessage(sendLogId, singletonList(mail), null, null, accountId, nickname, title, content);
  40 + }
  41 +
  42 + /**
  43 + * 发送 {@link MailSendMessage} 消息
  44 + *
  45 + * @param sendLogId 发送日志编码
  46 + * @param toMails 接收邮件地址
  47 + * @param ccMails 抄送邮件地址
  48 + * @param bccMails 密送邮件地址
  49 + * @param accountId 邮件账号编号
  50 + * @param nickname 邮件发件人
  51 + * @param title 邮件标题
  52 + * @param content 邮件内容
  53 + */
  54 + public void sendMailSendMessage(Long sendLogId, List<String> toMails, List<String> ccMails, List<String> bccMails,
  55 + Long accountId, String nickname, String title, String content) {
35 56 MailSendMessage message = new MailSendMessage()
36   - .setLogId(sendLogId).setMail(mail).setAccountId(accountId)
37   - .setNickname(nickname).setTitle(title).setContent(content);
  57 + .setLogId(sendLogId)
  58 + .setToMails(toMails).setCcMails(ccMails).setBccMails(bccMails)
  59 + .setAccountId(accountId).setNickname(nickname)
  60 + .setTitle(title).setContent(content);
38 61 applicationContext.publishEvent(message);
39 62 }
40 63  
... ...
yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/service/mail/MailLogService.java
... ... @@ -6,6 +6,7 @@ import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailAccountDO;
6 6 import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailLogDO;
7 7 import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailTemplateDO;
8 8  
  9 +import java.util.List;
9 10 import java.util.Map;
10 11  
11 12 /**
... ... @@ -50,6 +51,25 @@ public interface MailLogService {
50 51 String templateContent, Map<String, Object> templateParams, Boolean isSend);
51 52  
52 53 /**
  54 + * 创建邮件日志
  55 + *
  56 + * @param userId 用户编码
  57 + * @param userType 用户类型
  58 + * @param toMails 收件人邮件
  59 + * @param ccMails 收件人邮件
  60 + * @param bccMails 收件人邮件
  61 + * @param account 邮件账号信息
  62 + * @param template 模版信息
  63 + * @param templateContent 模版内容
  64 + * @param templateParams 模版参数
  65 + * @param isSend 是否发送成功
  66 + * @return 日志编号
  67 + */
  68 + Long createMailLog(Long userId, Integer userType, List<String> toMails, List<String> ccMails, List<String> bccMails,
  69 + MailAccountDO account, MailTemplateDO template,
  70 + String templateContent, Map<String, Object> templateParams, Boolean isSend);
  71 +
  72 + /**
53 73 * 更新邮件发送结果
54 74 *
55 75 * @param logId 日志编号
... ...
yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/service/mail/MailLogServiceImpl.java
1 1 package cn.iocoder.yudao.module.system.service.mail;
2 2  
  3 +import cn.hutool.core.collection.CollUtil;
3 4 import cn.iocoder.yudao.framework.common.pojo.PageResult;
4 5 import cn.iocoder.yudao.module.system.controller.admin.mail.vo.log.MailLogPageReqVO;
5 6 import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailAccountDO;
... ... @@ -12,6 +13,7 @@ import org.springframework.validation.annotation.Validated;
12 13  
13 14 import jakarta.annotation.Resource;
14 15 import java.time.LocalDateTime;
  16 +import java.util.List;
15 17 import java.util.Map;
16 18 import java.util.Objects;
17 19  
... ... @@ -62,6 +64,31 @@ public class MailLogServiceImpl implements MailLogService {
62 64 }
63 65  
64 66 @Override
  67 + public Long createMailLog(Long userId, Integer userType, List<String> toMails, List<String> ccMails, List<String> bccMails,
  68 + MailAccountDO account, MailTemplateDO template,
  69 + String templateContent, Map<String, Object> templateParams, Boolean isSend) {
  70 + String toMail = CollUtil.isEmpty(toMails) ? "" : String.join(",", toMails);
  71 + String ccMail = CollUtil.isEmpty(ccMails) ? "" : String.join(",", ccMails);
  72 + String bccMail = CollUtil.isEmpty(bccMails) ? "" : String.join(",", bccMails);
  73 +
  74 + MailLogDO.MailLogDOBuilder logDOBuilder = MailLogDO.builder();
  75 + // 根据是否要发送,设置状态
  76 + logDOBuilder.sendStatus(Objects.equals(isSend, true) ? MailSendStatusEnum.INIT.getStatus()
  77 + : MailSendStatusEnum.IGNORE.getStatus())
  78 + // 用户信息
  79 + .userId(userId).userType(userType).toMail(toMail).ccMail(ccMail).bccMail(bccMail)
  80 + .accountId(account.getId()).fromMail(account.getMail())
  81 + // 模板相关字段
  82 + .templateId(template.getId()).templateCode(template.getCode()).templateNickname(template.getNickname())
  83 + .templateTitle(template.getTitle()).templateContent(templateContent).templateParams(templateParams);
  84 +
  85 + // 插入数据库
  86 + MailLogDO logDO = logDOBuilder.build();
  87 + mailLogMapper.insert(logDO);
  88 + return logDO.getId();
  89 + }
  90 +
  91 + @Override
65 92 public void updateMailSendResult(Long logId, String messageId, Exception exception) {
66 93 // 1. 成功
67 94 if (exception == null) {
... ...
yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/service/mail/MailSendService.java
... ... @@ -2,6 +2,7 @@ package cn.iocoder.yudao.module.system.service.mail;
2 2  
3 3 import cn.iocoder.yudao.module.system.mq.message.mail.MailSendMessage;
4 4  
  5 +import java.util.List;
5 6 import java.util.Map;
6 7  
7 8 /**
... ... @@ -50,6 +51,49 @@ public interface MailSendService {
50 51 String templateCode, Map<String, Object> templateParams);
51 52  
52 53 /**
  54 + * 发送多条邮件给管理后台的用户
  55 + *
  56 + * @param toMails 收件邮箱
  57 + * @param ccMails 抄送邮箱
  58 + * @param bccMails 密送邮箱
  59 + * @param userId 用户编码
  60 + * @param templateCode 邮件模版编码
  61 + * @param templateParams 邮件模版参数
  62 + * @return 发送日志编号
  63 + */
  64 + Long sendMultipleMailToAdmin(List<String> toMails, List<String> ccMails, List<String> bccMails,
  65 + Long userId, String templateCode, Map<String, Object> templateParams);
  66 +
  67 + /**
  68 + * 发送多条邮件给用户 APP 的用户
  69 + *
  70 + * @param toMails 收件邮箱
  71 + * @param ccMails 抄送邮箱
  72 + * @param bccMails 密送邮箱
  73 + * @param userId 用户编码
  74 + * @param templateCode 邮件模版编码
  75 + * @param templateParams 邮件模版参数
  76 + * @return 发送日志编号
  77 + */
  78 + Long sendMultipleMailToMember(List<String> toMails, List<String> ccMails, List<String> bccMails,
  79 + Long userId, String templateCode, Map<String, Object> templateParams);
  80 +
  81 + /**
  82 + * 发送单条邮件给用户
  83 + *
  84 + * @param toMails 收件邮箱
  85 + * @param ccMails 抄送邮箱
  86 + * @param bccMails 密送邮箱
  87 + * @param userId 用户编码
  88 + * @param userType 用户类型
  89 + * @param templateCode 邮件模版编码
  90 + * @param templateParams 邮件模版参数
  91 + * @return 发送日志编号
  92 + */
  93 + Long sendMultipleMail(List<String> toMails, List<String> ccMails, List<String> bccMails,
  94 + Long userId, Integer userType, String templateCode, Map<String, Object> templateParams);
  95 +
  96 + /**
53 97 * 执行真正的邮件发送
54 98 * 注意,该方法仅仅提供给 MQ Consumer 使用
55 99 *
... ...
yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/service/mail/MailSendServiceImpl.java
1 1 package cn.iocoder.yudao.module.system.service.mail;
2 2  
  3 +import cn.hutool.core.collection.CollUtil;
3 4 import cn.hutool.core.util.StrUtil;
4 5 import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
5 6 import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
... ... @@ -17,10 +18,12 @@ import org.dromara.hutool.extra.mail.*;
17 18 import org.springframework.stereotype.Service;
18 19 import org.springframework.validation.annotation.Validated;
19 20  
  21 +import java.util.List;
20 22 import java.util.Map;
21 23  
22 24 import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
23 25 import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;
  26 +import static java.util.Collections.singletonList;
24 27  
25 28 /**
26 29 * 邮箱发送 Service 实现类
... ... @@ -100,13 +103,77 @@ public class MailSendServiceImpl implements MailSendService {
100 103 }
101 104  
102 105 @Override
  106 + public Long sendMultipleMailToAdmin(List<String> toMails, List<String> ccMails, List<String> bccMails, Long userId, String templateCode, Map<String, Object> templateParams) {
  107 + // 如果 mail 为空,则加载用户编号对应的邮箱
  108 + if (CollUtil.isEmpty(toMails)) {
  109 + AdminUserDO user = adminUserService.getUser(userId);
  110 + if (user != null) {
  111 + toMails = singletonList(user.getEmail());
  112 + }
  113 + }
  114 + // 执行发送
  115 + return sendMultipleMail(toMails, ccMails, bccMails, userId, UserTypeEnum.ADMIN.getValue(), templateCode, templateParams);
  116 + }
  117 +
  118 + @Override
  119 + public Long sendMultipleMailToMember(List<String> toMails, List<String> ccMails, List<String> bccMails, Long userId, String templateCode, Map<String, Object> templateParams) {
  120 + // 如果 mail 为空,则加载用户编号对应的邮箱
  121 + if (CollUtil.isEmpty(toMails)) {
  122 + toMails = singletonList(memberService.getMemberUserEmail(userId));
  123 + }
  124 + // 执行发送
  125 + return sendMultipleMail(toMails, ccMails, bccMails, userId, UserTypeEnum.MEMBER.getValue(), templateCode, templateParams);
  126 + }
  127 +
  128 + @Override
  129 + public Long sendMultipleMail(List<String> toMails, List<String> ccMails, List<String> bccMails, Long userId, Integer userType, String templateCode, Map<String, Object> templateParams) {
  130 + // 校验邮箱模版是否合法
  131 + MailTemplateDO template = validateMailTemplate(templateCode);
  132 + // 校验邮箱账号是否合法
  133 + MailAccountDO account = validateMailAccount(template.getAccountId());
  134 + // 校验邮件参数是否缺失
  135 + validateTemplateParams(template, templateParams);
  136 +
  137 + if (CollUtil.isEmpty(toMails)) {
  138 + throw exception(MAIL_SEND_MAIL_NOT_EXISTS);
  139 + }
  140 + // 校验邮箱是否存在
  141 + for (String mail : toMails) {
  142 + validateMail(mail);
  143 + }
  144 + if (CollUtil.isNotEmpty(ccMails)) {
  145 + for (String mail : ccMails) {
  146 + validateMail(mail);
  147 + }
  148 + }
  149 + if (CollUtil.isNotEmpty(bccMails)) {
  150 + for (String mail : bccMails) {
  151 + validateMail(mail);
  152 + }
  153 + }
  154 +
  155 + // 创建发送日志。如果模板被禁用,则不发送短信,只记录日志
  156 + Boolean isSend = CommonStatusEnum.ENABLE.getStatus().equals(template.getStatus());
  157 + String title = mailTemplateService.formatMailTemplateContent(template.getTitle(), templateParams);
  158 + String content = mailTemplateService.formatMailTemplateContent(template.getContent(), templateParams);
  159 + Long sendLogId = mailLogService.createMailLog(userId, userType, toMails, ccMails, bccMails,
  160 + account, template, content, templateParams, isSend);
  161 + // 发送 MQ 消息,异步执行发送短信
  162 + if (isSend) {
  163 + mailProducer.sendMailSendMessage(sendLogId, toMails, ccMails, bccMails, account.getId(),
  164 + template.getNickname(), title, content);
  165 + }
  166 + return sendLogId;
  167 + }
  168 +
  169 + @Override
103 170 public void doSendMail(MailSendMessage message) {
104 171 // 1. 创建发送账号
105 172 MailAccountDO account = validateMailAccount(message.getAccountId());
106 173 MailAccount mailAccount = buildMailAccount(account, message.getNickname());
107 174 // 2. 发送邮件
108 175 try {
109   - String messageId = MailUtil.send(mailAccount, message.getMail(),
  176 + String messageId = MailUtil.send(mailAccount, message.getToMails(), message.getCcMails(), message.getBccMails(),
110 177 message.getTitle(), message.getContent(), true);
111 178 // 3. 更新结果(成功)
112 179 mailLogService.updateMailSendResult(message.getLogId(), messageId, null);
... ... @@ -155,7 +222,7 @@ public class MailSendServiceImpl implements MailSendService {
155 222 }
156 223  
157 224 /**
158   - * 校验邮件参数是否确实
  225 + * 校验邮件参数是否缺失
159 226 *
160 227 * @param template 邮箱模板
161 228 * @param templateParams 参数列表
... ...