|
@@ -19,7 +19,6 @@ import java.io.File;
|
|
|
import java.util.Date;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
-import java.util.UUID;
|
|
|
import java.util.regex.Matcher;
|
|
|
import java.util.regex.Pattern;
|
|
|
import java.util.stream.Collectors;
|
|
@@ -28,6 +27,7 @@ import java.util.stream.Collectors;
|
|
|
public class ReadEmailService {
|
|
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(ReadEmailService.class);
|
|
|
+
|
|
|
private final CoreMailApiService coreMailApiService;
|
|
|
|
|
|
public ReadEmailService(CoreMailApiService coreMailApiService) {
|
|
@@ -61,40 +61,59 @@ public class ReadEmailService {
|
|
|
// 获取邮件
|
|
|
List<CoreMailInfoDTO> coreMailInfoDTOList = coreMailApiService.getFolderMails(token, cookie, folderId);
|
|
|
// 过滤符合解析要求的邮件
|
|
|
- List<CoreMailInfoDTO> readEmailDTOList = coreMailInfoDTOList.stream()
|
|
|
- .filter(e -> e.getFid().equals(folderId))
|
|
|
- .filter(e -> filterBySubject(e.getSubject()))
|
|
|
- .filter(e -> filterByEmailDate(e.getReceivedDate(), startDate, endDate)).toList();
|
|
|
+ List<CoreMailInfoDTO> readEmailDTOList = CollUtil.newArrayList();
|
|
|
+ for (CoreMailInfoDTO coreMailInfoDTO : coreMailInfoDTOList) {
|
|
|
+ String emailDate = DateUtil.format(coreMailInfoDTO.getSentDate(), DateConst.YYYY_MM_DD_HH_MM_SS);
|
|
|
+ if (!coreMailInfoDTO.getFid().equals(folderId)) {
|
|
|
+ log.info("非收件箱邮件,不解析 -> 邮件主题:{}, 日期:{}", coreMailInfoDTO.getSubject(), emailDate);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (!filterBySubject(coreMailInfoDTO.getSubject())) {
|
|
|
+ log.info("主题不满足解析条件 -> 邮件主题:{}, 日期:{}", coreMailInfoDTO.getSubject(), emailDate);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (!filterByEmailDate(coreMailInfoDTO.getReceivedDate(), startDate, endDate)) {
|
|
|
+ log.info("日期不满足解析条件 -> 邮件主题:{}, 日期:{}", coreMailInfoDTO.getSubject(), emailDate);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ readEmailDTOList.add(coreMailInfoDTO);
|
|
|
+ }
|
|
|
+
|
|
|
if (CollUtil.isEmpty(readEmailDTOList)) {
|
|
|
- log.error("获取不到邮件");
|
|
|
+ log.info("获取不到满足解析的条件邮件");
|
|
|
return emailKeyFileMap;
|
|
|
}
|
|
|
- Map<String, CoreMailInfoDTO> emailIdInfoMap = coreMailInfoDTOList.stream()
|
|
|
- .collect(Collectors.toMap(CoreMailInfoDTO::getId, e -> e));
|
|
|
+ Map<String, CoreMailInfoDTO> emailIdInfoMap = coreMailInfoDTOList.stream().collect(Collectors.toMap(CoreMailInfoDTO::getId, e -> e));
|
|
|
// 根据邮件id获取邮件内容
|
|
|
List<String> mailIdList = readEmailDTOList.stream().map(CoreMailInfoDTO::getId).toList();
|
|
|
for (String mailId : mailIdList) {
|
|
|
+ CoreMailInfoDTO coreMailInfoDTO = emailIdInfoMap.get(mailId);
|
|
|
+ String emailDate = DateUtil.format(coreMailInfoDTO.getSentDate(), DateConst.YYYY_MM_DD_HH_MM_SS);
|
|
|
String mid = mailId.split(":")[1];
|
|
|
// 获取邮件附件id(一封邮件可能有多个附件)
|
|
|
List<CoreMailAttachmentDTO> attachmentDTOList = coreMailApiService.getAttachmentOfMail(token, cookie, mid);
|
|
|
if (CollUtil.isEmpty(attachmentDTOList)) {
|
|
|
+ log.info("邮件没有附件,不解析 -> 邮件主题:{}, 日期:{}", coreMailInfoDTO.getSubject(), emailDate);
|
|
|
continue;
|
|
|
}
|
|
|
- String emailKey = UUID.randomUUID().toString().replace("-", "");
|
|
|
- CoreMailInfoDTO coreMailInfoDTO = emailIdInfoMap.get(mailId);
|
|
|
List<EmailContentInfoDTO> contentInfoDTOList = CollUtil.newArrayList();
|
|
|
// 根据附件id下载附件
|
|
|
for (CoreMailAttachmentDTO coreMailAttachmentDTO : attachmentDTOList) {
|
|
|
int attachmentId = coreMailAttachmentDTO.getId();
|
|
|
String filename = coreMailAttachmentDTO.getFilename();
|
|
|
+ if (StrUtil.isBlank(filename)) {
|
|
|
+ log.info("邮件附件名称为空 -> 邮件主题:{}, 日期:{}", coreMailInfoDTO.getSubject(), emailDate);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
File file = coreMailApiService.getFileFromAttachmentMail(token, cookie, mid, attachmentId, filename);
|
|
|
if (file == null) {
|
|
|
+ log.error("邮件附件下载失败 -> mid:{}, 邮件主题:{}, 日期:{}", mid, coreMailInfoDTO.getSubject(), emailDate);
|
|
|
continue;
|
|
|
}
|
|
|
EmailContentInfoDTO contentInfoDTO = new EmailContentInfoDTO();
|
|
|
contentInfoDTO.setEmailAddress(account);
|
|
|
contentInfoDTO.setEmailTitle(coreMailInfoDTO.getSubject());
|
|
|
- contentInfoDTO.setEmailDate(DateUtil.format(coreMailInfoDTO.getReceivedDate(), DateConst.YYYY_MM_DD_HH_MM_SS));
|
|
|
+ contentInfoDTO.setEmailDate(emailDate);
|
|
|
contentInfoDTO.setFileName(filename);
|
|
|
contentInfoDTO.setFilePath(file.getAbsolutePath());
|
|
|
contentInfoDTO.setSenderEmail(extractEmail(coreMailInfoDTO.getFrom()));
|
|
@@ -102,14 +121,14 @@ public class ReadEmailService {
|
|
|
contentInfoDTOList.add(contentInfoDTO);
|
|
|
}
|
|
|
if (CollUtil.isNotEmpty(contentInfoDTOList)) {
|
|
|
- emailKeyFileMap.put(emailKey, contentInfoDTOList);
|
|
|
+ emailKeyFileMap.put(mid, contentInfoDTOList);
|
|
|
}
|
|
|
}
|
|
|
return emailKeyFileMap;
|
|
|
}
|
|
|
|
|
|
private String extractEmail(String address) {
|
|
|
- if(StrUtil.isEmpty(address)) {
|
|
|
+ if (StrUtil.isEmpty(address)) {
|
|
|
return "";
|
|
|
}
|
|
|
Pattern pattern = Pattern.compile("<([^>]+)>");
|