支持附件以链接的形式发送
This commit is contained in:
@@ -31,6 +31,8 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||||||
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.client.RestTemplate;
|
||||||
import com.ruoyi.oa.mapper.OaFurnitureTableMapper;
|
import com.ruoyi.oa.mapper.OaFurnitureTableMapper;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
@@ -141,87 +143,106 @@ public class OaEmailAccountServiceImpl implements IOaEmailAccountService {
|
|||||||
if (account == null) {
|
if (account == null) {
|
||||||
return "发件人邮箱账号不存在";
|
return "发件人邮箱账号不存在";
|
||||||
}
|
}
|
||||||
// 通过furnitureIds查找所有邮箱
|
|
||||||
List<String> emailList = oaFurnitureTableMapper.selectEmailsByFurnitureIds(request.getFurnitureIds());
|
List<String> emailList = oaFurnitureTableMapper.selectEmailsByFurnitureIds(request.getFurnitureIds());
|
||||||
int type = account.getType() == null ? -1 : account.getType().intValue();
|
int type = account.getType() == null ? -1 : account.getType().intValue();
|
||||||
int success = 0, fail = 0;
|
int success = 0, fail = 0;
|
||||||
StringBuilder failList = new StringBuilder();
|
StringBuilder failList = new StringBuilder();
|
||||||
if (type == 0 || type == 1) {
|
// 处理附件:如果是http/https链接,先下载
|
||||||
// 网易/QQ邮箱 SMTP方式
|
List<File> attachmentFiles = new ArrayList<>();
|
||||||
for (String to : emailList) {
|
try {
|
||||||
try {
|
if (request.getAttachmentPaths() != null && !request.getAttachmentPaths().isEmpty()) {
|
||||||
boolean isHtml = request.getContent() != null &&
|
for (String path : request.getAttachmentPaths()) {
|
||||||
(request.getContent().contains("<html") ||
|
if (path.startsWith("http://") || path.startsWith("https://")) {
|
||||||
request.getContent().contains("<div") ||
|
attachmentFiles.add(EmailUtil.downloadUrlToTempFile(path));
|
||||||
request.getContent().contains("<p>"));
|
|
||||||
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());
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
if (isHtml) {
|
attachmentFiles.add(new File(path));
|
||||||
emailUtil.sendHtmlMailWithDynamicConfig(account, to, request.getSubject(), request.getContent());
|
|
||||||
} else {
|
|
||||||
emailUtil.sendMailWithDynamicConfig(account, to, request.getSubject(), request.getContent());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
success++;
|
|
||||||
} catch (Exception e) {
|
|
||||||
fail++;
|
|
||||||
failList.append(to).append(", ");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (type == 2) {
|
if (type == 0 || type == 1) {
|
||||||
for (String to : emailList) {
|
for (String to : emailList) {
|
||||||
try {
|
try {
|
||||||
boolean result = sendAliyunMail(account, to, request.getSubject(), request.getContent());
|
boolean isHtml = request.getContent() != null &&
|
||||||
if (result) {
|
(request.getContent().contains("<html") ||
|
||||||
|
request.getContent().contains("<div") ||
|
||||||
|
request.getContent().contains("<p>"));
|
||||||
|
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++;
|
success++;
|
||||||
} else {
|
} catch (Exception e) {
|
||||||
fail++;
|
fail++;
|
||||||
failList.append(to).append(", ");
|
failList.append(to).append(", ");
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
|
||||||
fail++;
|
|
||||||
failList.append(to).append(", ");
|
|
||||||
}
|
}
|
||||||
}
|
} else if (type == 2) {
|
||||||
} else if (type == 3) {
|
for (String to : emailList) {
|
||||||
for (String to : emailList) {
|
try {
|
||||||
try {
|
boolean result = sendAliyunMail(account, to, request.getSubject(), request.getContent());
|
||||||
boolean result = sendFeishuMail(account, to, request.getSubject(), request.getContent());
|
if (result) {
|
||||||
if (result) {
|
success++;
|
||||||
success++;
|
} else {
|
||||||
} else {
|
fail++;
|
||||||
|
failList.append(to).append(", ");
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
fail++;
|
fail++;
|
||||||
failList.append(to).append(", ");
|
failList.append(to).append(", ");
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
}
|
||||||
fail++;
|
} else if (type == 3) {
|
||||||
failList.append(to).append(", ");
|
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) {
|
if (success > 0 && request.getFurnitureIds() != null) {
|
||||||
for (Long furnitureId : request.getFurnitureIds()) {
|
for (Long furnitureId : request.getFurnitureIds()) {
|
||||||
oaFurnitureTableMapper.updateEmailSendInfo(furnitureId);
|
oaFurnitureTableMapper.updateEmailSendInfo(furnitureId);
|
||||||
@@ -229,6 +250,15 @@ public class OaEmailAccountServiceImpl implements IOaEmailAccountService {
|
|||||||
}
|
}
|
||||||
return "发送成功" + success + "封,失败" + fail + "封" + (fail > 0 ? (",失败邮箱:" + failList) : "");
|
return "发送成功" + success + "封,失败" + fail + "封" + (fail > 0 ? (",失败邮箱:" + failList) : "");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 辅助方法:File列表转路径列表
|
||||||
|
private List<String> toPathList(List<File> files) {
|
||||||
|
List<String> 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) {
|
private boolean sendAliyunMail(OaEmailAccount account, String to, String subject, String content) {
|
||||||
try {
|
try {
|
||||||
DefaultProfile profile = DefaultProfile.getProfile(
|
DefaultProfile profile = DefaultProfile.getProfile(
|
||||||
|
|||||||
@@ -355,4 +355,22 @@ public class EmailUtil {
|
|||||||
|
|
||||||
return processedHtml;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user