|
@@ -33,8 +33,7 @@ import com.smppw.modaq.domain.mapper.EmailParseInfoMapper;
|
|
import com.smppw.modaq.infrastructure.util.ExcelUtil;
|
|
import com.smppw.modaq.infrastructure.util.ExcelUtil;
|
|
import com.smppw.modaq.infrastructure.util.FileUtil;
|
|
import com.smppw.modaq.infrastructure.util.FileUtil;
|
|
import jakarta.mail.*;
|
|
import jakarta.mail.*;
|
|
-import jakarta.mail.internet.MimeMessage;
|
|
|
|
-import jakarta.mail.internet.MimeMultipart;
|
|
|
|
|
|
+import jakarta.mail.internet.MimeUtility;
|
|
import jakarta.mail.search.ComparisonTerm;
|
|
import jakarta.mail.search.ComparisonTerm;
|
|
import jakarta.mail.search.ReceivedDateTerm;
|
|
import jakarta.mail.search.ReceivedDateTerm;
|
|
import jakarta.mail.search.SearchTerm;
|
|
import jakarta.mail.search.SearchTerm;
|
|
@@ -47,7 +46,6 @@ import org.springframework.util.StopWatch;
|
|
|
|
|
|
import java.io.File;
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
-import java.nio.file.Files;
|
|
|
|
import java.nio.file.Path;
|
|
import java.nio.file.Path;
|
|
import java.nio.file.Paths;
|
|
import java.nio.file.Paths;
|
|
import java.util.*;
|
|
import java.util.*;
|
|
@@ -82,7 +80,6 @@ public class EmailParseService {
|
|
EmailFileInfoMapper emailFileInfoMapper,
|
|
EmailFileInfoMapper emailFileInfoMapper,
|
|
ReportParserFactory reportParserFactory,
|
|
ReportParserFactory reportParserFactory,
|
|
ReportWriterFactory reportWriterFactory) {
|
|
ReportWriterFactory reportWriterFactory) {
|
|
-// this.emailFieldMapper = emailFieldMapper;
|
|
|
|
this.emailParseInfoMapper = emailParseInfoMapper;
|
|
this.emailParseInfoMapper = emailParseInfoMapper;
|
|
this.emailFileInfoMapper = emailFileInfoMapper;
|
|
this.emailFileInfoMapper = emailFileInfoMapper;
|
|
this.reportParserFactory = reportParserFactory;
|
|
this.reportParserFactory = reportParserFactory;
|
|
@@ -236,7 +233,7 @@ public class EmailParseService {
|
|
if (CollUtil.isNotEmpty(zipFiles)) {
|
|
if (CollUtil.isNotEmpty(zipFiles)) {
|
|
for (EmailZipFileDTO zipFile : zipFiles) {
|
|
for (EmailZipFileDTO zipFile : zipFiles) {
|
|
EmailFileInfoDO emailFile = saveEmailFileInfo(emailId, null, zipFile.getFilename(), zipFile.getFilepath(), null);
|
|
EmailFileInfoDO emailFile = saveEmailFileInfo(emailId, null, zipFile.getFilename(), zipFile.getFilepath(), null);
|
|
- // 解析结果(可以从python获取或者自行解析)并保存报告
|
|
|
|
|
|
+ // 解析并保存报告
|
|
ParseResult<ReportData> parseResult = this.parseReportAndHandleResult(emailFile.getId(), zipFile.getFilename(),
|
|
ParseResult<ReportData> parseResult = this.parseReportAndHandleResult(emailFile.getId(), zipFile.getFilename(),
|
|
zipFile.getFilepath(), zipFile.getEmailType(), emailFile.getAiFileId());
|
|
zipFile.getFilepath(), zipFile.getEmailType(), emailFile.getAiFileId());
|
|
dataList.add(parseResult);
|
|
dataList.add(parseResult);
|
|
@@ -245,7 +242,7 @@ public class EmailParseService {
|
|
String fileName = emailContentInfoDTO.getFileName();
|
|
String fileName = emailContentInfoDTO.getFileName();
|
|
EmailFileInfoDO emailFile = saveEmailFileInfo(emailId, emailContentInfoDTO.getFileId(), fileName,
|
|
EmailFileInfoDO emailFile = saveEmailFileInfo(emailId, emailContentInfoDTO.getFileId(), fileName,
|
|
emailContentInfoDTO.getFilePath(), emailContentInfoDTO.getAiFileId());
|
|
emailContentInfoDTO.getFilePath(), emailContentInfoDTO.getAiFileId());
|
|
- // 解析结果(可以从python获取或者自行解析)并保存报告
|
|
|
|
|
|
+ // 解析并保存报告
|
|
ParseResult<ReportData> parseResult = this.parseReportAndHandleResult(emailFile.getId(), fileName,
|
|
ParseResult<ReportData> parseResult = this.parseReportAndHandleResult(emailFile.getId(), fileName,
|
|
emailContentInfoDTO.getFilePath(), emailContentInfoDTO.getEmailType(), emailFile.getAiFileId());
|
|
emailContentInfoDTO.getFilePath(), emailContentInfoDTO.getEmailType(), emailFile.getAiFileId());
|
|
dataList.add(parseResult);
|
|
dataList.add(parseResult);
|
|
@@ -277,7 +274,9 @@ public class EmailParseService {
|
|
String filepath, Integer emailType, String aiFileId) {
|
|
String filepath, Integer emailType, String aiFileId) {
|
|
ParseResult<ReportData> result = new ParseResult<>();
|
|
ParseResult<ReportData> result = new ParseResult<>();
|
|
boolean reportFlag = !Objects.equals(EmailTypeConst.REPORT_EMAIL_TYPE, emailType)
|
|
boolean reportFlag = !Objects.equals(EmailTypeConst.REPORT_EMAIL_TYPE, emailType)
|
|
- && !Objects.equals(EmailTypeConst.REPORT_LETTER_EMAIL_TYPE, emailType);
|
|
|
|
|
|
+ && !Objects.equals(EmailTypeConst.REPORT_LETTER_EMAIL_TYPE, emailType)
|
|
|
|
+ && !Objects.equals(EmailTypeConst.REPORT_WEEKLy_TYPE, emailType)
|
|
|
|
+ && !Objects.equals(EmailTypeConst.REPORT_OTHER_TYPE, emailType);
|
|
if (reportFlag || StrUtil.isBlank(fileName) || fileName.endsWith(".html")) {
|
|
if (reportFlag || StrUtil.isBlank(fileName) || fileName.endsWith(".html")) {
|
|
result.setStatus(ReportParseStatus.NOT_A_REPORT.getCode());
|
|
result.setStatus(ReportParseStatus.NOT_A_REPORT.getCode());
|
|
result.setMsg(StrUtil.format(ReportParseStatus.NOT_A_REPORT.getMsg(), fileName));
|
|
result.setMsg(StrUtil.format(ReportParseStatus.NOT_A_REPORT.getMsg(), fileName));
|
|
@@ -294,7 +293,7 @@ public class EmailParseService {
|
|
if (Objects.equals(EmailTypeConst.REPORT_LETTER_EMAIL_TYPE, emailType)) {
|
|
if (Objects.equals(EmailTypeConst.REPORT_LETTER_EMAIL_TYPE, emailType)) {
|
|
reportType = ReportType.LETTER;
|
|
reportType = ReportType.LETTER;
|
|
}
|
|
}
|
|
- // 解析器--如果开启python解析则直接调用python接口,否则根据文件后缀获取对应解析器
|
|
|
|
|
|
+ // 解析器--根据文件后缀获取对应解析器,解析不了就用AI来解析
|
|
ReportParserFileType fileType;
|
|
ReportParserFileType fileType;
|
|
String fileSuffix = StrUtil.subAfter(fileName, ".", true);
|
|
String fileSuffix = StrUtil.subAfter(fileName, ".", true);
|
|
fileType = ReportParserFileType.getBySuffix(fileSuffix);
|
|
fileType = ReportParserFileType.getBySuffix(fileSuffix);
|
|
@@ -310,13 +309,14 @@ public class EmailParseService {
|
|
result.setMsg(StrUtil.format(ReportParseStatus.NOT_A_REPORT.getMsg(), fileName));
|
|
result.setMsg(StrUtil.format(ReportParseStatus.NOT_A_REPORT.getMsg(), fileName));
|
|
return result;
|
|
return result;
|
|
}
|
|
}
|
|
|
|
+ // 不支持解析的格式文件
|
|
|
|
+ boolean notSupportFile = false;
|
|
// 解析报告
|
|
// 解析报告
|
|
ReportData reportData = null;
|
|
ReportData reportData = null;
|
|
- boolean notSupportFile = false;
|
|
|
|
StopWatch parserWatch = new StopWatch();
|
|
StopWatch parserWatch = new StopWatch();
|
|
parserWatch.start();
|
|
parserWatch.start();
|
|
try {
|
|
try {
|
|
- if (StrUtil.isBlank(aiFileId)) {
|
|
|
|
|
|
+ if (StrUtil.isBlank(aiFileId) && reportType != ReportType.OTHER && reportType != ReportType.WEEKLY) {
|
|
ReportParserParams params = ReportParserParams.builder().fileId(fileId).filename(fileName).filepath(filepath)
|
|
ReportParserParams params = ReportParserParams.builder().fileId(fileId).filename(fileName).filepath(filepath)
|
|
.registerNumber(registerNumber).reportType(reportType).build();
|
|
.registerNumber(registerNumber).reportType(reportType).build();
|
|
ReportParser<ReportData> instance = this.reportParserFactory.getInstance(reportType, fileType);
|
|
ReportParser<ReportData> instance = this.reportParserFactory.getInstance(reportType, fileType);
|
|
@@ -325,8 +325,14 @@ public class EmailParseService {
|
|
result.setMsg("报告解析成功");
|
|
result.setMsg("报告解析成功");
|
|
result.setData(reportData);
|
|
result.setData(reportData);
|
|
} else {
|
|
} else {
|
|
- if (log.isInfoEnabled()) {
|
|
|
|
- log.info("报告{} 是已经存在ai解析记录,上传过文件{},直接跳转到AI解析器进行解析", fileName, fileId);
|
|
|
|
|
|
+ if (reportType == ReportType.OTHER || reportType == ReportType.WEEKLY) {
|
|
|
|
+ if (log.isInfoEnabled()) {
|
|
|
|
+ log.info("报告{} 是周报或其他类型,直接用AI解析器解析", fileName);
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ if (log.isInfoEnabled()) {
|
|
|
|
+ log.info("报告{} 是已经存在ai解析记录,上传过文件{},直接跳转到AI解析器进行解析", fileName, fileId);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} catch (ReportParseException e) {
|
|
} catch (ReportParseException e) {
|
|
@@ -352,8 +358,12 @@ public class EmailParseService {
|
|
try {
|
|
try {
|
|
reportData = instance.parse(params);
|
|
reportData = instance.parse(params);
|
|
result.setStatus(1);
|
|
result.setStatus(1);
|
|
- result.setMsg("报告解析成功");
|
|
|
|
|
|
+ result.setMsg("报告解析成功--AI");
|
|
result.setData(reportData);
|
|
result.setData(reportData);
|
|
|
|
+ } catch (ReportParseException e) {
|
|
|
|
+ log.error("AI解析失败:{}", StrUtil.format(e.getMsg(), fileName));
|
|
|
|
+ result.setStatus(e.getCode());
|
|
|
|
+ result.setMsg(StrUtil.format(e.getMsg(), fileName));
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
log.error("AI解析错误:{}", ExceptionUtil.stacktraceToString(e));
|
|
log.error("AI解析错误:{}", ExceptionUtil.stacktraceToString(e));
|
|
result.setStatus(ReportParseStatus.PARSE_FAIL.getCode());
|
|
result.setStatus(ReportParseStatus.PARSE_FAIL.getCode());
|
|
@@ -440,6 +450,7 @@ public class EmailParseService {
|
|
emailParseInfoDO.setEmailTitle(emailContentInfoDTO.getEmailTitle());
|
|
emailParseInfoDO.setEmailTitle(emailContentInfoDTO.getEmailTitle());
|
|
emailParseInfoDO.setEmailType(emailContentInfoDTO.getEmailType());
|
|
emailParseInfoDO.setEmailType(emailContentInfoDTO.getEmailType());
|
|
emailParseInfoDO.setParseStatus(EmailParseStatusConst.SUCCESS);
|
|
emailParseInfoDO.setParseStatus(EmailParseStatusConst.SUCCESS);
|
|
|
|
+ emailParseInfoDO.setAttrSize(emailContentInfoDTO.getFileSize());
|
|
emailParseInfoDO.setIsvalid(1);
|
|
emailParseInfoDO.setIsvalid(1);
|
|
emailParseInfoDO.setCreatorId(0);
|
|
emailParseInfoDO.setCreatorId(0);
|
|
emailParseInfoDO.setCreateTime(new Date());
|
|
emailParseInfoDO.setCreateTime(new Date());
|
|
@@ -450,15 +461,14 @@ public class EmailParseService {
|
|
|
|
|
|
public Map<Integer, List<String>> getEmailType() {
|
|
public Map<Integer, List<String>> getEmailType() {
|
|
Map<Integer, List<String>> emailTypeMap = MapUtil.newHashMap(3, true);
|
|
Map<Integer, List<String>> emailTypeMap = MapUtil.newHashMap(3, true);
|
|
-// EmailTypeRuleDO emailTypeRuleDO = emailTypeRuleMapper.getEmailTypeRule();
|
|
|
|
-// String nav = emailTypeRuleDO != null && StrUtil.isNotBlank(emailTypeRuleDO.getNav()) ? emailTypeRuleDO.getNav() : emailRuleConfig.getNav();
|
|
|
|
-// String valuation = emailTypeRuleDO != null && StrUtil.isNotBlank(emailTypeRuleDO.getValuation()) ? emailTypeRuleDO.getValuation() : emailRuleConfig.getValuation();
|
|
|
|
-// String report = emailTypeRuleDO != null && StrUtil.isNotBlank(emailTypeRuleDO.getReport()) ? emailTypeRuleDO.getReport() : emailRuleConfig.getReport();
|
|
|
|
-// emailTypeMap.put(EmailTypeConst.VALUATION_EMAIL_TYPE, Arrays.stream(valuation.split(",")).toList());
|
|
|
|
-// emailTypeMap.put(EmailTypeConst.NAV_EMAIL_TYPE, Arrays.stream(nav.split(",")).toList());
|
|
|
|
-// emailTypeMap.put(EmailTypeConst.REPORT_EMAIL_TYPE, ListUtil.toList("月报", "周报", "月度报告"));
|
|
|
|
|
|
+ emailTypeMap.put(EmailTypeConst.REPORT_EMAIL_TYPE,
|
|
|
|
+ ListUtil.toList("月报", "月度报告", "季报", "季度报告", "年报", "年度报告"));
|
|
emailTypeMap.put(EmailTypeConst.REPORT_LETTER_EMAIL_TYPE,
|
|
emailTypeMap.put(EmailTypeConst.REPORT_LETTER_EMAIL_TYPE,
|
|
- ListUtil.toList("确认单", "确认函", "交易确认数据", "赎回确认", "申购确认", "分红确认", "确认表", "交易确认", "确认"));
|
|
|
|
|
|
+ ListUtil.toList(ReportType.LETTER.getPatterns()));
|
|
|
|
+ emailTypeMap.put(EmailTypeConst.REPORT_OTHER_TYPE,
|
|
|
|
+ ListUtil.toList(ReportType.OTHER.getPatterns()));
|
|
|
|
+ emailTypeMap.put(EmailTypeConst.REPORT_WEEKLy_TYPE,
|
|
|
|
+ ListUtil.toList(ReportType.WEEKLY.getPatterns()));
|
|
return emailTypeMap;
|
|
return emailTypeMap;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -487,16 +497,14 @@ public class EmailParseService {
|
|
return MapUtil.newHashMap();
|
|
return MapUtil.newHashMap();
|
|
}
|
|
}
|
|
Map<String, List<EmailContentInfoDTO>> emailMessageMap = MapUtil.newHashMap();
|
|
Map<String, List<EmailContentInfoDTO>> emailMessageMap = MapUtil.newHashMap();
|
|
- for (Message message1 : messages) {
|
|
|
|
|
|
+ for (Message message : messages) {
|
|
long start = System.currentTimeMillis();
|
|
long start = System.currentTimeMillis();
|
|
|
|
+ List<EmailContentInfoDTO> emailContentInfoDTOList = CollUtil.newArrayList();
|
|
|
|
+ String uuidKey = UUID.randomUUID().toString().replaceAll("-", "");
|
|
|
|
+ Integer emailType;
|
|
|
|
+ String senderEmail;
|
|
String emailTitle = null;
|
|
String emailTitle = null;
|
|
try {
|
|
try {
|
|
- MimeMessage message = (MimeMessage) message1;
|
|
|
|
- emailTitle = message.getSubject();
|
|
|
|
- List<EmailContentInfoDTO> emailContentInfoDTOList = CollUtil.newArrayList();
|
|
|
|
- String uuidKey = UUID.randomUUID().toString().replaceAll("-", "");
|
|
|
|
- Integer emailType;
|
|
|
|
- String senderEmail;
|
|
|
|
Date emailDate = message.getSentDate();
|
|
Date emailDate = message.getSentDate();
|
|
String emailDateStr = DateUtil.format(emailDate, DateConst.YYYY_MM_DD_HH_MM_SS);
|
|
String emailDateStr = DateUtil.format(emailDate, DateConst.YYYY_MM_DD_HH_MM_SS);
|
|
if (log.isInfoEnabled()) {
|
|
if (log.isInfoEnabled()) {
|
|
@@ -507,6 +515,7 @@ public class EmailParseService {
|
|
if (isNotParseConditionSatisfied) {
|
|
if (isNotParseConditionSatisfied) {
|
|
continue;
|
|
continue;
|
|
}
|
|
}
|
|
|
|
+ emailTitle = message.getSubject();
|
|
senderEmail = getSenderEmail(message);
|
|
senderEmail = getSenderEmail(message);
|
|
emailType = EmailUtil.getEmailTypeBySubject(emailTitle, emailTypeMap);
|
|
emailType = EmailUtil.getEmailTypeBySubject(emailTitle, emailTypeMap);
|
|
if (emailType == null) {
|
|
if (emailType == null) {
|
|
@@ -515,48 +524,28 @@ public class EmailParseService {
|
|
}
|
|
}
|
|
log.info("邮件{} 基本信息获取完成,开始下载附件!邮件日期:{}", emailTitle, emailDateStr);
|
|
log.info("邮件{} 基本信息获取完成,开始下载附件!邮件日期:{}", emailTitle, emailDateStr);
|
|
Object content = message.getContent();
|
|
Object content = message.getContent();
|
|
- // 1.邮件为MIME多部分消息体:可能既有邮件又有正文
|
|
|
|
- if (content instanceof MimeMultipart) {
|
|
|
|
- emailContentInfoDTOList = EmailUtil.collectMimeMultipart(message, mailboxInfoDTO.getAccount(), path);
|
|
|
|
- }
|
|
|
|
- // 2.邮件只有正文
|
|
|
|
- if (content instanceof String) {
|
|
|
|
- EmailContentInfoDTO emailContentInfoDTO = new EmailContentInfoDTO();
|
|
|
|
- emailContentInfoDTO.setEmailContent(content.toString());
|
|
|
|
- emailContentInfoDTO.setEmailDate(emailDateStr);
|
|
|
|
- emailContentInfoDTO.setEmailTitle(emailTitle);
|
|
|
|
- String fileName = emailTitle + DateUtil.format(emailDate, DateConst.YYYYMMDDHHMMSS24);
|
|
|
|
- String filePath = path + mailboxInfoDTO.getAccount() + File.separator + DateUtil.format(emailDate, DateConst.YYYY_MM_DD) + File.separator + fileName + ".html";
|
|
|
|
- File saveFile = new File(filePath);
|
|
|
|
- saveFile.setReadable(true);
|
|
|
|
- if (!saveFile.exists()) {
|
|
|
|
- if (!saveFile.getParentFile().exists()) {
|
|
|
|
- Files.createDirectories(saveFile.getParentFile().toPath());
|
|
|
|
- saveFile.getParentFile().setExecutable(true);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- FileUtil.writeFile(filePath, content.toString());
|
|
|
|
- emailContentInfoDTO.setFilePath(filePath);
|
|
|
|
- emailContentInfoDTO.setFileName(fileName);
|
|
|
|
- emailContentInfoDTOList.add(emailContentInfoDTO);
|
|
|
|
|
|
+
|
|
|
|
+ if (content instanceof Multipart multipart) {
|
|
|
|
+ this.reMultipart(mailboxInfoDTO.getAccount(), emailTitle, emailDate, multipart, emailContentInfoDTOList);
|
|
|
|
+ } else if (content instanceof Part part) {
|
|
|
|
+ this.rePart(mailboxInfoDTO.getAccount(), emailTitle, emailDate, part, emailContentInfoDTOList);
|
|
|
|
+ } else {
|
|
|
|
+ log.warn("不支持的邮件数据 {}", emailTitle);
|
|
}
|
|
}
|
|
|
|
+
|
|
if (CollUtil.isNotEmpty(emailContentInfoDTOList)) {
|
|
if (CollUtil.isNotEmpty(emailContentInfoDTOList)) {
|
|
-// // 估值表或定期报告邮件不展示正文html文件
|
|
|
|
-// if (emailType.equals(EmailTypeConst.VALUATION_EMAIL_TYPE) || emailType.equals(EmailTypeConst.REPORT_EMAIL_TYPE)) {
|
|
|
|
-// emailContentInfoDTOList = emailContentInfoDTOList.stream().filter(e -> !ExcelUtil.isHTML(e.getFilePath())).toList();
|
|
|
|
-// }
|
|
|
|
emailContentInfoDTOList.forEach(e -> {
|
|
emailContentInfoDTOList.forEach(e -> {
|
|
e.setEmailType(emailType);
|
|
e.setEmailType(emailType);
|
|
e.setSenderEmail(senderEmail);
|
|
e.setSenderEmail(senderEmail);
|
|
});
|
|
});
|
|
emailMessageMap.put(uuidKey, emailContentInfoDTOList);
|
|
emailMessageMap.put(uuidKey, emailContentInfoDTOList);
|
|
}
|
|
}
|
|
- log.info("邮件{} 内容为:{}", emailTitle, emailContentInfoDTOList);
|
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
- log.error("获取邮箱的邮件报错,堆栈信息:{}", ExceptionUtil.stacktraceToString(e));
|
|
|
|
|
|
+ log.error("获取邮箱的邮件{} 报错,堆栈信息:{}", emailTitle, ExceptionUtil.stacktraceToString(e));
|
|
} finally {
|
|
} finally {
|
|
- if (log.isInfoEnabled()) {
|
|
|
|
- log.info("邮件{} 下载完成,总计耗时{} ms", emailTitle, System.currentTimeMillis() - start);
|
|
|
|
|
|
+ if (log.isInfoEnabled() && emailTitle != null) {
|
|
|
|
+ log.info("邮件{} 下载完成,总计耗时{} ms,文件内容如下\n {}",
|
|
|
|
+ emailTitle, System.currentTimeMillis() - start, emailContentInfoDTOList);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -565,12 +554,63 @@ public class EmailParseService {
|
|
return emailMessageMap;
|
|
return emailMessageMap;
|
|
}
|
|
}
|
|
|
|
|
|
- private String getSenderEmail(MimeMessage message) {
|
|
|
|
- Address[] senderAddress = null;
|
|
|
|
|
|
+ private void rePart(String account, String subject, Date sendDate, Part part, List<EmailContentInfoDTO> emailContentInfoDTOList) throws Exception {
|
|
|
|
+ String disposition = part.getDisposition();
|
|
|
|
+ if (disposition != null && (disposition.equals(Part.ATTACHMENT) || disposition.equals(Part.INLINE))) {
|
|
|
|
+ String emailDate = DateUtil.format(sendDate, DateConst.YYYYMMDDHHMMSS24);
|
|
|
|
+ String emailDateStr = DateUtil.format(sendDate, DateConst.YYYYMMDD);
|
|
|
|
+ String filePath = path + File.separator + account + File.separator + emailDateStr + File.separator;
|
|
|
|
+
|
|
|
|
+ EmailContentInfoDTO emailContentInfoDTO = new EmailContentInfoDTO();
|
|
|
|
+
|
|
|
|
+ String fileName = MimeUtility.decodeText(part.getFileName());
|
|
|
|
+ emailContentInfoDTO.setFileName(fileName);
|
|
|
|
+ emailContentInfoDTO.setFileSize(part.getSize());
|
|
|
|
+
|
|
|
|
+ if (log.isInfoEnabled()) {
|
|
|
|
+ log.info("邮件{} 大小:{}byte 开始下载。。。。。", subject, part.getSize());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ String realPath = filePath + emailDate + fileName;
|
|
|
|
+
|
|
|
|
+ File saveFile = cn.hutool.core.io.FileUtil.file(realPath);
|
|
|
|
+ if (!saveFile.exists()) {
|
|
|
|
+ if (!saveFile.getParentFile().exists()) {
|
|
|
|
+ boolean mkdirs = saveFile.getParentFile().mkdirs();
|
|
|
|
+ if (!mkdirs) {
|
|
|
|
+ log.warn("file path mkdir failed.");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ FileUtil.saveFile(saveFile, part);
|
|
|
|
+ } else {
|
|
|
|
+ cn.hutool.core.io.FileUtil.del(saveFile);
|
|
|
|
+ FileUtil.saveFile(saveFile, part);
|
|
|
|
+ }
|
|
|
|
+ emailContentInfoDTO.setFilePath(saveFile.getAbsolutePath());
|
|
|
|
+
|
|
|
|
+ emailContentInfoDTO.setEmailAddress(account);
|
|
|
|
+ emailContentInfoDTO.setEmailTitle(subject);
|
|
|
|
+ emailContentInfoDTO.setEmailDate(DateUtil.format(sendDate, DateConst.YYYY_MM_DD_HH_MM_SS));
|
|
|
|
+ emailContentInfoDTOList.add(emailContentInfoDTO);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private void reMultipart(String account, String subject, Date emailDate, Multipart multipart, List<EmailContentInfoDTO> emailContentInfoDTOList) throws Exception {
|
|
|
|
+ for (int i = 0; i < multipart.getCount(); i++) {
|
|
|
|
+ Part bodyPart = multipart.getBodyPart(i);
|
|
|
|
+ if (bodyPart.getContent() instanceof Multipart mp) {
|
|
|
|
+ this.reMultipart(account, subject, emailDate, mp, emailContentInfoDTOList);
|
|
|
|
+ } else {
|
|
|
|
+ this.rePart(account, subject, emailDate, bodyPart, emailContentInfoDTOList);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private String getSenderEmail(Message message) {
|
|
|
|
+ Address[] senderAddress;
|
|
try {
|
|
try {
|
|
senderAddress = message.getFrom();
|
|
senderAddress = message.getFrom();
|
|
if (senderAddress == null || senderAddress.length == 0) {
|
|
if (senderAddress == null || senderAddress.length == 0) {
|
|
- log.info("发件人获取失败=============================");
|
|
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|
|
// 此时的address是含有编码(MIME编码方式)后的文本和实际的邮件地址
|
|
// 此时的address是含有编码(MIME编码方式)后的文本和实际的邮件地址
|
|
@@ -581,24 +621,23 @@ public class EmailParseService {
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- log.info("发件人地址:" + address + "========================senderAddress size:" + senderAddress.length);
|
|
|
|
// 正则表达式匹配邮件地址
|
|
// 正则表达式匹配邮件地址
|
|
Pattern pattern = Pattern.compile("<(\\S+)>");
|
|
Pattern pattern = Pattern.compile("<(\\S+)>");
|
|
Matcher matcher = pattern.matcher(address);
|
|
Matcher matcher = pattern.matcher(address);
|
|
if (matcher.find()) {
|
|
if (matcher.find()) {
|
|
return matcher.group(1);
|
|
return matcher.group(1);
|
|
}
|
|
}
|
|
- //说明匹配不到,直接获取sender
|
|
|
|
- Address sender = message.getSender();
|
|
|
|
- if (sender == null) {
|
|
|
|
- return address;
|
|
|
|
- }
|
|
|
|
- String senderEmail = sender.toString();
|
|
|
|
- log.info("senderEmail:" + senderEmail + "====================");
|
|
|
|
- if (senderEmail.contains("<") && senderEmail.contains(">") && senderEmail.indexOf("<") < senderEmail.indexOf(">")) {
|
|
|
|
- senderEmail = senderEmail.substring(senderEmail.indexOf("<") + 1, senderEmail.length() - 1);
|
|
|
|
- }
|
|
|
|
- return senderEmail;
|
|
|
|
|
|
+// //说明匹配不到,直接获取sender
|
|
|
|
+// Address sender = message.getSender();
|
|
|
|
+// if (sender == null) {
|
|
|
|
+// return address;
|
|
|
|
+// }
|
|
|
|
+// String senderEmail = sender.toString();
|
|
|
|
+// log.info("senderEmail:" + senderEmail + "====================");
|
|
|
|
+// if (senderEmail.contains("<") && senderEmail.contains(">") && senderEmail.indexOf("<") < senderEmail.indexOf(">")) {
|
|
|
|
+// senderEmail = senderEmail.substring(senderEmail.indexOf("<") + 1, senderEmail.length() - 1);
|
|
|
|
+// }
|
|
|
|
+// return senderEmail;
|
|
} catch (MessagingException e) {
|
|
} catch (MessagingException e) {
|
|
log.error(e.getMessage(), e);
|
|
log.error(e.getMessage(), e);
|
|
}
|
|
}
|