|
@@ -1,8 +1,11 @@
|
|
|
package com.citu.module.system.service.mail;
|
|
|
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
+import cn.hutool.core.io.FileUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
import cn.hutool.extra.mail.MailAccount;
|
|
|
import cn.hutool.extra.mail.MailUtil;
|
|
|
+import cn.hutool.http.HttpUtil;
|
|
|
import com.citu.framework.common.enums.CommonStatusEnum;
|
|
|
import com.citu.framework.common.enums.UserTypeEnum;
|
|
|
import com.citu.module.system.dal.dataobject.mail.MailAccountDO;
|
|
@@ -18,6 +21,9 @@ import org.springframework.stereotype.Service;
|
|
|
import org.springframework.validation.annotation.Validated;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
+import java.io.File;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
import static com.citu.framework.common.exception.util.ServiceExceptionUtil.exception;
|
|
@@ -49,6 +55,49 @@ public class MailSendServiceImpl implements MailSendService {
|
|
|
@Resource
|
|
|
private MailProducer mailProducer;
|
|
|
|
|
|
+ /**
|
|
|
+ * 下载文件并保留原始文件名
|
|
|
+ *
|
|
|
+ * @param fileUrls 文件URL列表
|
|
|
+ * @return 下载的文件列表
|
|
|
+ */
|
|
|
+ private static List<File> downloadFilesFromUrls(List<String> fileUrls) {
|
|
|
+ if (CollUtil.isEmpty(fileUrls)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ List<File> tempFiles = new ArrayList<>();
|
|
|
+ for (String fileUrl : fileUrls) {
|
|
|
+ try {
|
|
|
+ // 从URL中提取文件名
|
|
|
+ String fileName = StrUtil.subAfter(fileUrl, "/", true);
|
|
|
+ if (StrUtil.isBlank(fileName)) {
|
|
|
+ fileName = "unknown_file"; // 如果URL中没有文件名,使用默认名称
|
|
|
+ }
|
|
|
+
|
|
|
+ // 创建临时文件
|
|
|
+ File tempFile = FileUtil.createTempFile(null, null, true);
|
|
|
+
|
|
|
+ // 使用 Hutool 的 HttpUtil.downloadFile 下载文件到临时文件
|
|
|
+ long size = HttpUtil.downloadFile(fileUrl, tempFile);
|
|
|
+
|
|
|
+ if (size > 0) {
|
|
|
+ // 将临时文件重命名为原始文件名
|
|
|
+ File targetFile = new File(tempFile.getParent(), fileName);
|
|
|
+ FileUtil.rename(tempFile, targetFile.getName(), true);
|
|
|
+
|
|
|
+ log.info("文件下载成功: " + fileUrl + ", 文件大小: " + size + " 字节");
|
|
|
+ tempFiles.add(targetFile);
|
|
|
+ } else {
|
|
|
+ log.info("文件下载失败: " + fileUrl);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ log.info("文件下载失败: " + fileUrl);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return tempFiles;
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public Long sendSingleMailToAdmin(String mail, Long userId,
|
|
|
String templateCode, Map<String, Object> templateParams) {
|
|
@@ -60,7 +109,7 @@ public class MailSendServiceImpl implements MailSendService {
|
|
|
}
|
|
|
}
|
|
|
// 执行发送
|
|
|
- return sendSingleMail(mail, userId, UserTypeEnum.ADMIN.getValue(), templateCode, templateParams);
|
|
|
+ return sendSingleMail(mail, userId, UserTypeEnum.ADMIN.getValue(), templateCode, templateParams, null);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -71,12 +120,12 @@ public class MailSendServiceImpl implements MailSendService {
|
|
|
mail = memberService.getMemberUserEmail(userId);
|
|
|
}
|
|
|
// 执行发送
|
|
|
- return sendSingleMail(mail, userId, UserTypeEnum.MEMBER.getValue(), templateCode, templateParams);
|
|
|
+ return sendSingleMail(mail, userId, UserTypeEnum.MEMBER.getValue(), templateCode, templateParams,null);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public Long sendSingleMail(String mail, Long userId, Integer userType,
|
|
|
- String templateCode, Map<String, Object> templateParams) {
|
|
|
+ String templateCode, Map<String, Object> templateParams,List<String> fileUrl) {
|
|
|
// 校验邮箱模版是否合法
|
|
|
MailTemplateDO template = validateMailTemplate(templateCode);
|
|
|
// 校验邮箱账号是否合法
|
|
@@ -95,7 +144,7 @@ public class MailSendServiceImpl implements MailSendService {
|
|
|
// 发送 MQ 消息,异步执行发送短信
|
|
|
if (isSend) {
|
|
|
mailProducer.sendMailSendMessage(sendLogId, mail, account.getId(),
|
|
|
- template.getNickname(), title, content);
|
|
|
+ template.getNickname(), title, content,fileUrl);
|
|
|
}
|
|
|
return sendLogId;
|
|
|
}
|
|
@@ -104,16 +153,31 @@ public class MailSendServiceImpl implements MailSendService {
|
|
|
public void doSendMail(MailSendMessage message) {
|
|
|
// 1. 创建发送账号
|
|
|
MailAccountDO account = validateMailAccount(message.getAccountId());
|
|
|
- MailAccount mailAccount = buildMailAccount(account, message.getNickname());
|
|
|
+ MailAccount mailAccount = buildMailAccount(account, message.getNickname());
|
|
|
// 2. 发送邮件
|
|
|
+
|
|
|
+ // 下载所有文件到本地临时文件
|
|
|
+ List<File> tempFiles = downloadFilesFromUrls(message.getFileUrl());
|
|
|
try {
|
|
|
- String messageId = MailUtil.send(mailAccount, message.getMail(),
|
|
|
- message.getTitle(), message.getContent(), true);
|
|
|
+ String messageId = null;
|
|
|
+ if (CollUtil.isEmpty(tempFiles)) {
|
|
|
+ // 无附件
|
|
|
+ messageId = MailUtil.send(mailAccount, message.getMail(),
|
|
|
+ message.getTitle(), message.getContent(), true);
|
|
|
+ } else {
|
|
|
+ // 加入附件
|
|
|
+ messageId = MailUtil.send(mailAccount, message.getMail(),
|
|
|
+ message.getTitle(), message.getContent(), true, tempFiles.toArray(new File[0]));
|
|
|
+ }
|
|
|
// 3. 更新结果(成功)
|
|
|
mailLogService.updateMailSendResult(message.getLogId(), messageId, null);
|
|
|
} catch (Exception e) {
|
|
|
// 3. 更新结果(异常)
|
|
|
mailLogService.updateMailSendResult(message.getLogId(), null, e);
|
|
|
+ } finally {
|
|
|
+ if (CollUtil.isNotEmpty(tempFiles)) {
|
|
|
+ tempFiles.forEach(FileUtil::del);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -158,7 +222,7 @@ public class MailSendServiceImpl implements MailSendService {
|
|
|
/**
|
|
|
* 校验邮件参数是否确实
|
|
|
*
|
|
|
- * @param template 邮箱模板
|
|
|
+ * @param template 邮箱模板
|
|
|
* @param templateParams 参数列表
|
|
|
*/
|
|
|
@VisibleForTesting
|