refactor(common-mail): 使用 MailBuilder 替代 MailUtils

- 新增链式 MailBuilder,统一邮件发送入口
  - 删除 MailUtils,业务代码全部切换为 builder 调用
  - 默认邮件账户改为发送时获取,避免类加载阶段依赖 Bean
  - 自定义发件账号时复制默认配置,避免污染全局 MailAccount
  - 增加收件人、标题和正文参数校验
  - 调整内联图片流关闭时机,发送结束后统一关闭
  - 将 MailAccount 构建逻辑下沉到 MailProperties
This commit is contained in:
疯狂的狮子Li
2026-05-16 14:25:34 +08:00
parent f4b4c710c5
commit 99db6dceea
7 changed files with 382 additions and 494 deletions
@@ -2,7 +2,7 @@ package org.dromara.demo.controller;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.mail.utils.MailUtils;
import org.dromara.common.mail.core.MailBuilder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@@ -32,7 +32,7 @@ public class MailSendController {
*/
@GetMapping("/sendSimpleMessage")
public R<Void> sendSimpleMessage(String to, String subject, String text) {
MailUtils.sendText(to, subject, text);
MailBuilder.of().to(to).subject(subject).text(text).send();
return R.ok();
}
@@ -46,7 +46,7 @@ public class MailSendController {
@GetMapping("/sendMessageWithAttachment")
public R<Void> sendMessageWithAttachment(String to, String subject, String text) {
// 附件路径 禁止前端传递 有任意读取系统文件风险
MailUtils.sendText(to, subject, text, new File("/xxx/xxx"));
MailBuilder.of().to(to).subject(subject).text(text).files(new File("/xxx/xxx")).send();
return R.ok();
}
@@ -62,7 +62,7 @@ public class MailSendController {
// 附件路径 禁止前端传递 有任意读取系统文件风险
String[] paths = new String[]{"/xxx/xxx", "/xxx/xxx"};
File[] array = Arrays.stream(paths).map(File::new).toArray(File[]::new);
MailUtils.sendText(to, subject, text, array);
MailBuilder.of().to(to).subject(subject).text(text).files(array).send();
return R.ok();
}
@@ -11,7 +11,7 @@ import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.SpringUtils;
import org.dromara.common.core.utils.StreamUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mail.utils.MailUtils;
import org.dromara.common.mail.core.MailBuilder;
import org.dromara.system.api.MessageService;
import org.dromara.system.api.domain.PushPayloadDTO;
import org.dromara.system.api.domain.UserDTO;
@@ -122,7 +122,7 @@ public class FlwCommonServiceImpl implements IFlwCommonService {
message, null, path
));
}
case EMAIL_MESSAGE -> MailUtils.sendText(emails, subject, message);
case EMAIL_MESSAGE -> MailBuilder.of().to(emails).subject(subject).text(message).send();
case SMS_MESSAGE -> {
// LinkedHashMap<String, String> map = new LinkedHashMap<>(1);
// // 根据具体短信服务商参数用法传参