From 2ca29936131ae4039ee3451649a1a19483b6f875 Mon Sep 17 00:00:00 2001 From: Joshi <3040996759@qq.com> Date: Wed, 16 Jul 2025 16:05:08 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E9=99=84=E4=BB=B6=E4=BB=A5?= =?UTF-8?q?=E9=93=BE=E6=8E=A5=E7=9A=84=E5=BD=A2=E5=BC=8F=E5=8F=91=E9=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../impl/OaEmailAccountServiceImpl.java | 154 +++++++++++------- .../java/com/ruoyi/oa/utils/EmailUtil.java | 18 ++ 2 files changed, 110 insertions(+), 62 deletions(-) diff --git a/ruoyi-oa/src/main/java/com/ruoyi/oa/service/impl/OaEmailAccountServiceImpl.java b/ruoyi-oa/src/main/java/com/ruoyi/oa/service/impl/OaEmailAccountServiceImpl.java index 6ae7a09..fba95ae 100644 --- a/ruoyi-oa/src/main/java/com/ruoyi/oa/service/impl/OaEmailAccountServiceImpl.java +++ b/ruoyi-oa/src/main/java/com/ruoyi/oa/service/impl/OaEmailAccountServiceImpl.java @@ -31,6 +31,8 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.client.RestTemplate; import com.ruoyi.oa.mapper.OaFurnitureTableMapper; +import java.io.File; +import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Collection; @@ -141,87 +143,106 @@ public class OaEmailAccountServiceImpl implements IOaEmailAccountService { if (account == null) { return "发件人邮箱账号不存在"; } - // 通过furnitureIds查找所有邮箱 List emailList = oaFurnitureTableMapper.selectEmailsByFurnitureIds(request.getFurnitureIds()); int type = account.getType() == null ? -1 : account.getType().intValue(); int success = 0, fail = 0; StringBuilder failList = new StringBuilder(); - if (type == 0 || type == 1) { - // 网易/QQ邮箱 SMTP方式 - for (String to : emailList) { - try { - boolean isHtml = request.getContent() != null && - (request.getContent().contains("")); - boolean hasAttachments = request.getAttachmentPaths() != null && !request.getAttachmentPaths().isEmpty(); - boolean hasInlineImages = request.getInlineImagePaths() != null && !request.getInlineImagePaths().isEmpty(); - if (hasAttachments && hasInlineImages) { - if (isHtml) { - String processedContent = emailUtil.processHtmlWithInlineImages(request.getContent(), request.getInlineImagePaths()); - emailUtil.sendHtmlMailWithAttachmentsAndDynamicConfig(account, to, request.getSubject(), processedContent, request.getAttachmentPaths()); - } else { - emailUtil.sendMailWithAttachmentAndDynamicConfig(account, to, request.getSubject(), request.getContent(), request.getAttachmentPaths().get(0)); - } - } else if (hasAttachments) { - if (isHtml) { - emailUtil.sendHtmlMailWithAttachmentsAndDynamicConfig(account, to, request.getSubject(), request.getContent(), request.getAttachmentPaths()); - } else { - emailUtil.sendMailWithAttachmentAndDynamicConfig(account, to, request.getSubject(), request.getContent(), request.getAttachmentPaths().get(0)); - } - } else if (hasInlineImages) { - if (isHtml) { - emailUtil.sendHtmlMailWithInlineImagesAndDynamicConfig(account, to, request.getSubject(), request.getContent(), request.getInlineImagePaths()); - } else { - emailUtil.sendMailWithDynamicConfig(account, to, request.getSubject(), request.getContent()); - } + // 处理附件:如果是http/https链接,先下载 + List attachmentFiles = new ArrayList<>(); + try { + if (request.getAttachmentPaths() != null && !request.getAttachmentPaths().isEmpty()) { + for (String path : request.getAttachmentPaths()) { + if (path.startsWith("http://") || path.startsWith("https://")) { + attachmentFiles.add(EmailUtil.downloadUrlToTempFile(path)); } else { - if (isHtml) { - emailUtil.sendHtmlMailWithDynamicConfig(account, to, request.getSubject(), request.getContent()); - } else { - emailUtil.sendMailWithDynamicConfig(account, to, request.getSubject(), request.getContent()); - } + attachmentFiles.add(new File(path)); } - success++; - } catch (Exception e) { - fail++; - failList.append(to).append(", "); } } - } else if (type == 2) { - for (String to : emailList) { - try { - boolean result = sendAliyunMail(account, to, request.getSubject(), request.getContent()); - if (result) { + if (type == 0 || type == 1) { + for (String to : emailList) { + try { + boolean isHtml = request.getContent() != null && + (request.getContent().contains("")); + boolean hasAttachments = !attachmentFiles.isEmpty(); + boolean hasInlineImages = request.getInlineImagePaths() != null && !request.getInlineImagePaths().isEmpty(); + if (hasAttachments && hasInlineImages) { + if (isHtml) { + String processedContent = emailUtil.processHtmlWithInlineImages(request.getContent(), request.getInlineImagePaths()); + emailUtil.sendHtmlMailWithAttachmentsAndDynamicConfig(account, to, request.getSubject(), processedContent, toPathList(attachmentFiles)); + } else { + emailUtil.sendMailWithAttachmentAndDynamicConfig(account, to, request.getSubject(), request.getContent(), attachmentFiles.get(0).getAbsolutePath()); + } + } else if (hasAttachments) { + if (isHtml) { + emailUtil.sendHtmlMailWithAttachmentsAndDynamicConfig(account, to, request.getSubject(), request.getContent(), toPathList(attachmentFiles)); + } else { + emailUtil.sendMailWithAttachmentAndDynamicConfig(account, to, request.getSubject(), request.getContent(), attachmentFiles.get(0).getAbsolutePath()); + } + } else if (hasInlineImages) { + if (isHtml) { + emailUtil.sendHtmlMailWithInlineImagesAndDynamicConfig(account, to, request.getSubject(), request.getContent(), request.getInlineImagePaths()); + } else { + emailUtil.sendMailWithDynamicConfig(account, to, request.getSubject(), request.getContent()); + } + } else { + if (isHtml) { + emailUtil.sendHtmlMailWithDynamicConfig(account, to, request.getSubject(), request.getContent()); + } else { + emailUtil.sendMailWithDynamicConfig(account, to, request.getSubject(), request.getContent()); + } + } success++; - } else { + } catch (Exception e) { fail++; failList.append(to).append(", "); } - } catch (Exception e) { - fail++; - failList.append(to).append(", "); } - } - } else if (type == 3) { - for (String to : emailList) { - try { - boolean result = sendFeishuMail(account, to, request.getSubject(), request.getContent()); - if (result) { - success++; - } else { + } else if (type == 2) { + for (String to : emailList) { + try { + boolean result = sendAliyunMail(account, to, request.getSubject(), request.getContent()); + if (result) { + success++; + } else { + fail++; + failList.append(to).append(", "); + } + } catch (Exception e) { fail++; failList.append(to).append(", "); } - } catch (Exception e) { - fail++; - failList.append(to).append(", "); + } + } else if (type == 3) { + for (String to : emailList) { + try { + boolean result = sendFeishuMail(account, to, request.getSubject(), request.getContent()); + if (result) { + success++; + } else { + fail++; + failList.append(to).append(", "); + } + } catch (Exception e) { + fail++; + failList.append(to).append(", "); + } + } + } else { + return "不支持的邮箱类型"; + } + } catch (Exception e) { + return "附件处理失败:" + e.getMessage(); + } finally { + // 删除所有临时文件 + for (File file : attachmentFiles) { + if (file != null && file.exists() && file.getName().startsWith("mail_attach_")) { + file.delete(); } } - } else { - return "不支持的邮箱类型"; } - // 邮件发送逻辑结束后,统计success>0时批量更新所有furnitureId if (success > 0 && request.getFurnitureIds() != null) { for (Long furnitureId : request.getFurnitureIds()) { oaFurnitureTableMapper.updateEmailSendInfo(furnitureId); @@ -229,6 +250,15 @@ public class OaEmailAccountServiceImpl implements IOaEmailAccountService { } return "发送成功" + success + "封,失败" + fail + "封" + (fail > 0 ? (",失败邮箱:" + failList) : ""); } + + // 辅助方法:File列表转路径列表 + private List toPathList(List files) { + List paths = new ArrayList<>(); + for (File f : files) { + if (f != null) paths.add(f.getAbsolutePath()); + } + return paths; + } private boolean sendAliyunMail(OaEmailAccount account, String to, String subject, String content) { try { DefaultProfile profile = DefaultProfile.getProfile( diff --git a/ruoyi-oa/src/main/java/com/ruoyi/oa/utils/EmailUtil.java b/ruoyi-oa/src/main/java/com/ruoyi/oa/utils/EmailUtil.java index 5bdabe0..ae63a35 100644 --- a/ruoyi-oa/src/main/java/com/ruoyi/oa/utils/EmailUtil.java +++ b/ruoyi-oa/src/main/java/com/ruoyi/oa/utils/EmailUtil.java @@ -355,4 +355,22 @@ public class EmailUtil { return processedHtml; } + + /** + * 下载网络文件到本地临时文件 + * @param url 文件的http/https链接 + * @return 本地临时文件 + */ + public static File downloadUrlToTempFile(String url) throws Exception { + File tempFile = File.createTempFile("mail_attach_", null); + try (java.io.InputStream in = new java.net.URL(url).openStream(); + java.io.OutputStream out = new java.io.FileOutputStream(tempFile)) { + byte[] buffer = new byte[8192]; + int bytesRead; + while ((bytesRead = in.read(buffer)) != -1) { + out.write(buffer, 0, bytesRead); + } + } + return tempFile; + } } \ No newline at end of file