Commit ae9d3984 by zhangxingmin

push

parent 09eddd52
......@@ -12,12 +12,14 @@ import org.springframework.util.CollectionUtils;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.activation.URLDataSource;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.io.File;
import java.net.URL;
import java.util.Date;
import java.util.List;
import java.util.Properties;
......@@ -109,30 +111,34 @@ public class ApiEmailServiceImpl implements ApiEmailService {
// 将正文部分添加到多部分容器中
multipart.addBodyPart(messageBodyPart);
// 判断是否有附件路径
// 修改附件处理部分:
if (StringUtils.isNotBlank(dto.getAttachmentPath())) {
// 按分号分割多个附件路径
String[] attachmentPaths = dto.getAttachmentPath().split(";");
for (String attachmentPath : attachmentPaths) {
// 去除路径两端的空格
attachmentPath = attachmentPath.trim();
// 检查路径是否非空
if (StringUtils.isNotBlank(attachmentPath)) {
try {
// 创建附件部分
MimeBodyPart attachmentPart = new MimeBodyPart();
// 创建文件数据源
DataSource source = new FileDataSource(attachmentPath);
// 设置附件的数据处理器
DataSource source;
String fileName;
if (attachmentPath.startsWith("http://") || attachmentPath.startsWith("https://")) {
// 使用URLDataSource处理网络附件
URL url = new URL(attachmentPath);
source = new URLDataSource(url);
fileName = getFileNameFromUrl(attachmentPath);
} else {
// 处理本地文件附件
source = new FileDataSource(attachmentPath);
fileName = new File(attachmentPath).getName();
}
attachmentPart.setDataHandler(new DataHandler(source));
// 设置附件文件名(使用原文件名)
attachmentPart.setFileName(new File(attachmentPath).getName());
// 将附件部分添加到多部分容器中
attachmentPart.setFileName(fileName);
multipart.addBodyPart(attachmentPart);
log.info("添加附件: {}", attachmentPath);
} catch (Exception e) {
log.error("添加附件失败: {}", attachmentPath, e);
// 可以选择继续处理其他附件,或者抛出异常
throw new RuntimeException("添加附件失败: " + attachmentPath, e);
}
}
......@@ -156,4 +162,19 @@ public class ApiEmailServiceImpl implements ApiEmailService {
}
}
// 辅助方法:从URL中提取文件名
private String getFileNameFromUrl(String url) {
try {
return new File(new URL(url).getPath()).getName();
} catch (Exception e) {
return "attachment";
}
}
// 辅助方法:获取文件扩展名
private String getFileExtension(String filename) {
int lastDot = filename.lastIndexOf('.');
return (lastDot == -1) ? "" : filename.substring(lastDot);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment