|
@@ -1,29 +1,17 @@
|
|
|
package com.smppw.modaq.application.util;
|
|
|
|
|
|
-import cn.hutool.core.collection.CollUtil;
|
|
|
-import cn.hutool.core.date.DateUtil;
|
|
|
import cn.hutool.core.exceptions.ExceptionUtil;
|
|
|
import cn.hutool.core.map.MapUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
import cn.hutool.extra.mail.JakartaUserPassAuthenticator;
|
|
|
-import com.smppw.modaq.common.conts.DateConst;
|
|
|
import com.smppw.modaq.common.conts.EmailTypeConst;
|
|
|
-import com.smppw.modaq.domain.dto.EmailContentInfoDTO;
|
|
|
import com.smppw.modaq.domain.dto.MailboxInfoDTO;
|
|
|
-import com.smppw.modaq.infrastructure.util.ExcelUtil;
|
|
|
-import com.smppw.modaq.infrastructure.util.FileUtil;
|
|
|
import com.sun.mail.imap.IMAPStore;
|
|
|
-import jakarta.mail.Message;
|
|
|
-import jakarta.mail.MessagingException;
|
|
|
import jakarta.mail.Session;
|
|
|
import jakarta.mail.Store;
|
|
|
-import jakarta.mail.internet.MimeBodyPart;
|
|
|
-import jakarta.mail.internet.MimeMultipart;
|
|
|
-import jakarta.mail.internet.MimeUtility;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
-import java.io.File;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
@@ -41,100 +29,100 @@ public class EmailUtil {
|
|
|
private static final String POP3 = "pop3";
|
|
|
private static final String IMAP = "imap";
|
|
|
|
|
|
- /**
|
|
|
- * 采集邮件(多消息体)信息
|
|
|
- *
|
|
|
- * @param message 邮件
|
|
|
- * @param emailAddress 邮箱地址
|
|
|
- * @param path 存储路径
|
|
|
- * @return 从邮箱采集到的信息
|
|
|
- * @throws Exception 异常信息
|
|
|
- */
|
|
|
- public static List<EmailContentInfoDTO> collectMimeMultipart(Message message, String emailAddress, String path) throws Exception {
|
|
|
- List<EmailContentInfoDTO> emailContentInfoDTOList = CollUtil.newArrayList();
|
|
|
- String emailTitle = message.getSubject();
|
|
|
- String emailDate = DateUtil.format(message.getSentDate(), DateConst.YYYYMMDDHHMMSS24);
|
|
|
- String emailDateStr = DateUtil.format(message.getSentDate(), DateConst.YYYYMMDD);
|
|
|
- String filePath = path + File.separator + emailAddress + File.separator + emailDateStr + File.separator;
|
|
|
-
|
|
|
- MimeMultipart mimeMultipart = (MimeMultipart) message.getContent();
|
|
|
- int length = mimeMultipart.getCount();
|
|
|
- // 遍历邮件消息体 (我这里不要html正文)
|
|
|
- for (int i = 0; i < length; i++) {
|
|
|
- EmailContentInfoDTO emailContentInfoDTO = new EmailContentInfoDTO();
|
|
|
- MimeBodyPart part = (MimeBodyPart) mimeMultipart.getBodyPart(i);
|
|
|
- Object partContent = part.getContent();
|
|
|
- String contentClass = part.getContent().getClass().getSimpleName();
|
|
|
- // 1.邮件正文
|
|
|
- switch (contentClass) {
|
|
|
- case "String" -> {
|
|
|
- // 文件名 = 邮件主题 + 邮件日期
|
|
|
- String fileName = emailTitle + "_" + emailDate + ".html";
|
|
|
- String content = partContent.toString();
|
|
|
- emailContentInfoDTO = collectTextPart(part, content, filePath, fileName);
|
|
|
- }
|
|
|
- case "BASE64DecoderStream" -> {
|
|
|
- if (StrUtil.isNotBlank(part.getFileName())) {
|
|
|
- String fileName = MimeUtility.decodeText(part.getFileName());
|
|
|
- if (isSupportedFileType(fileName)) {
|
|
|
- continue;
|
|
|
- }
|
|
|
- emailContentInfoDTO.setFileName(fileName);
|
|
|
-
|
|
|
- String realPath = filePath + emailDate + fileName;
|
|
|
-
|
|
|
- File saveFile = cn.hutool.core.io.FileUtil.file(realPath);
|
|
|
- if (!saveFile.exists()) {
|
|
|
- if (!saveFile.getParentFile().exists()) {
|
|
|
- saveFile.getParentFile().mkdirs();
|
|
|
- }
|
|
|
- FileUtil.saveFile(saveFile, part);
|
|
|
- } else {
|
|
|
- cn.hutool.core.io.FileUtil.del(saveFile);
|
|
|
- FileUtil.saveFile(saveFile, part);
|
|
|
- }
|
|
|
- emailContentInfoDTO.setFilePath(realPath);
|
|
|
- }
|
|
|
- }
|
|
|
- case "MimeMultipart" -> {
|
|
|
- MimeMultipart contentPart = (MimeMultipart) partContent;
|
|
|
- int length2 = contentPart.getCount();
|
|
|
- for (int i2 = 0; i2 < length2; i2++) {
|
|
|
- part = (MimeBodyPart) contentPart.getBodyPart(i2);
|
|
|
- partContent = part.getContent();
|
|
|
- contentClass = partContent.getClass().getSimpleName();
|
|
|
- if ("String".equals(contentClass)) {
|
|
|
- // 文件名 = 邮件主题 + 邮件日期
|
|
|
- String fileName = emailTitle + "_" + emailDate + ".html";
|
|
|
- String content = partContent.toString();
|
|
|
- emailContentInfoDTO = collectTextPart(part, content, filePath, fileName);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- String filepath = emailContentInfoDTO.getFilePath();
|
|
|
- if (emailContentInfoDTO.getEmailContent() == null && filepath == null) {
|
|
|
- continue;
|
|
|
- }
|
|
|
- emailContentInfoDTO.setEmailAddress(emailAddress);
|
|
|
- emailContentInfoDTO.setEmailTitle(emailTitle);
|
|
|
- emailContentInfoDTO.setEmailDate(DateUtil.format(message.getSentDate(), DateConst.YYYY_MM_DD_HH_MM_SS));
|
|
|
- emailContentInfoDTOList.add(emailContentInfoDTO);
|
|
|
- }
|
|
|
-
|
|
|
- return emailContentInfoDTOList;
|
|
|
- }
|
|
|
+// /**
|
|
|
+// * 采集邮件(多消息体)信息
|
|
|
+// *
|
|
|
+// * @param message 邮件
|
|
|
+// * @param emailAddress 邮箱地址
|
|
|
+// * @param path 存储路径
|
|
|
+// * @return 从邮箱采集到的信息
|
|
|
+// * @throws Exception 异常信息
|
|
|
+// */
|
|
|
+// public static List<EmailContentInfoDTO> collectMimeMultipart(Message message, String emailAddress, String path) throws Exception {
|
|
|
+// List<EmailContentInfoDTO> emailContentInfoDTOList = CollUtil.newArrayList();
|
|
|
+// String emailTitle = message.getSubject();
|
|
|
+// String emailDate = DateUtil.format(message.getSentDate(), DateConst.YYYYMMDDHHMMSS24);
|
|
|
+// String emailDateStr = DateUtil.format(message.getSentDate(), DateConst.YYYYMMDD);
|
|
|
+// String filePath = path + File.separator + emailAddress + File.separator + emailDateStr + File.separator;
|
|
|
+//
|
|
|
+// MimeMultipart mimeMultipart = (MimeMultipart) message.getContent();
|
|
|
+// int length = mimeMultipart.getCount();
|
|
|
+// // 遍历邮件消息体 (我这里不要html正文)
|
|
|
+// for (int i = 0; i < length; i++) {
|
|
|
+// EmailContentInfoDTO emailContentInfoDTO = new EmailContentInfoDTO();
|
|
|
+// MimeBodyPart part = (MimeBodyPart) mimeMultipart.getBodyPart(i);
|
|
|
+// Object partContent = part.getContent();
|
|
|
+// String contentClass = part.getContent().getClass().getSimpleName();
|
|
|
+// // 1.邮件正文
|
|
|
+// switch (contentClass) {
|
|
|
+// case "String" -> {
|
|
|
+// // 文件名 = 邮件主题 + 邮件日期
|
|
|
+// String fileName = emailTitle + "_" + emailDate + ".html";
|
|
|
+// String content = partContent.toString();
|
|
|
+// emailContentInfoDTO = collectTextPart(part, content, filePath, fileName);
|
|
|
+// }
|
|
|
+// case "BASE64DecoderStream" -> {
|
|
|
+// if (StrUtil.isNotBlank(part.getFileName())) {
|
|
|
+// String fileName = MimeUtility.decodeText(part.getFileName());
|
|
|
+// if (isSupportedFileType(fileName)) {
|
|
|
+// continue;
|
|
|
+// }
|
|
|
+// emailContentInfoDTO.setFileName(fileName);
|
|
|
+//
|
|
|
+// String realPath = filePath + emailDate + fileName;
|
|
|
+//
|
|
|
+// File saveFile = cn.hutool.core.io.FileUtil.file(realPath);
|
|
|
+// if (!saveFile.exists()) {
|
|
|
+// if (!saveFile.getParentFile().exists()) {
|
|
|
+// saveFile.getParentFile().mkdirs();
|
|
|
+// }
|
|
|
+// FileUtil.saveFile(saveFile, part);
|
|
|
+// } else {
|
|
|
+// cn.hutool.core.io.FileUtil.del(saveFile);
|
|
|
+// FileUtil.saveFile(saveFile, part);
|
|
|
+// }
|
|
|
+// emailContentInfoDTO.setFilePath(realPath);
|
|
|
+// }
|
|
|
+// }
|
|
|
+// case "MimeMultipart" -> {
|
|
|
+// MimeMultipart contentPart = (MimeMultipart) partContent;
|
|
|
+// int length2 = contentPart.getCount();
|
|
|
+// for (int i2 = 0; i2 < length2; i2++) {
|
|
|
+// part = (MimeBodyPart) contentPart.getBodyPart(i2);
|
|
|
+// partContent = part.getContent();
|
|
|
+// contentClass = partContent.getClass().getSimpleName();
|
|
|
+// if ("String".equals(contentClass)) {
|
|
|
+// // 文件名 = 邮件主题 + 邮件日期
|
|
|
+// String fileName = emailTitle + "_" + emailDate + ".html";
|
|
|
+// String content = partContent.toString();
|
|
|
+// emailContentInfoDTO = collectTextPart(part, content, filePath, fileName);
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }
|
|
|
+// String filepath = emailContentInfoDTO.getFilePath();
|
|
|
+// if (emailContentInfoDTO.getEmailContent() == null && filepath == null) {
|
|
|
+// continue;
|
|
|
+// }
|
|
|
+// emailContentInfoDTO.setEmailAddress(emailAddress);
|
|
|
+// emailContentInfoDTO.setEmailTitle(emailTitle);
|
|
|
+// emailContentInfoDTO.setEmailDate(DateUtil.format(message.getSentDate(), DateConst.YYYY_MM_DD_HH_MM_SS));
|
|
|
+// emailContentInfoDTOList.add(emailContentInfoDTO);
|
|
|
+// }
|
|
|
+//
|
|
|
+// return emailContentInfoDTOList;
|
|
|
+// }
|
|
|
|
|
|
// private static List<EmailContentInfoDTO> zipFile(String filepath) {
|
|
|
// return null;
|
|
|
// }
|
|
|
|
|
|
- private static boolean isSupportedFileType(String fileName) {
|
|
|
- if (StrUtil.isBlank(fileName)) {
|
|
|
- return true;
|
|
|
- }
|
|
|
- return !ExcelUtil.isZip(fileName) && !ExcelUtil.isExcel(fileName) && !ExcelUtil.isPdf(fileName) && !ExcelUtil.isHTML(fileName) && !ExcelUtil.isRAR(fileName);
|
|
|
- }
|
|
|
+// private static boolean isSupportedFileType(String fileName) {
|
|
|
+// if (StrUtil.isBlank(fileName)) {
|
|
|
+// return true;
|
|
|
+// }
|
|
|
+// return !ExcelUtil.isZip(fileName) && !ExcelUtil.isExcel(fileName) && !ExcelUtil.isPdf(fileName) && !ExcelUtil.isHTML(fileName) && !ExcelUtil.isRAR(fileName);
|
|
|
+// }
|
|
|
|
|
|
// /**
|
|
|
// * 根据日期过滤邮件
|
|
@@ -163,78 +151,78 @@ public class EmailUtil {
|
|
|
// return messageList;
|
|
|
// }
|
|
|
|
|
|
- /**
|
|
|
- * 采集邮件正文
|
|
|
- *
|
|
|
- * @param part 邮件消息体
|
|
|
- * @param partContent 邮件消息内筒
|
|
|
- * @param filePath 文件路径
|
|
|
- * @param fileName 文件名
|
|
|
- * @return 采集到邮件正文(html格式包含table标签)
|
|
|
- */
|
|
|
- public static EmailContentInfoDTO collectTextPart(MimeBodyPart part, String partContent, String filePath, String fileName) {
|
|
|
- EmailContentInfoDTO emailContentInfoDTO = new EmailContentInfoDTO();
|
|
|
- try {
|
|
|
- if ((part.getContentType().contains("text/html") || part.getContentType().contains("TEXT/HTML"))) {
|
|
|
- emailContentInfoDTO.setEmailContent(partContent);
|
|
|
- String savePath = filePath + fileName;
|
|
|
- File saveFile = new File(savePath);
|
|
|
- if (!saveFile.exists()) {
|
|
|
- if (!saveFile.getParentFile().exists()) {
|
|
|
- saveFile.getParentFile().mkdirs();
|
|
|
- saveFile.getParentFile().setExecutable(true);
|
|
|
- }
|
|
|
- }
|
|
|
- //获取邮件编码
|
|
|
- String contentType = part.getContentType();
|
|
|
- String html = partContent.toString();
|
|
|
- try {
|
|
|
- if (contentType.contains("charset=")) {
|
|
|
- contentType = contentType.substring(contentType.indexOf("charset=") + 8).replaceAll("\"", "");
|
|
|
- html = html.replace("charset=" + contentType.toLowerCase(), "charset=UTF-8");
|
|
|
- html = html.replace("charset=" + contentType.toUpperCase(), "charset=UTF-8");
|
|
|
- }
|
|
|
- if (savePath.contains(":")) {
|
|
|
- savePath = savePath.replaceAll(":", "");
|
|
|
- }
|
|
|
- cn.hutool.core.io.FileUtil.writeUtf8String(html, new File(savePath));
|
|
|
- } catch (Exception e) {
|
|
|
- logger.error(e.getMessage(), e);
|
|
|
- }
|
|
|
- emailContentInfoDTO.setFileName(fileName);
|
|
|
- emailContentInfoDTO.setFilePath(savePath);
|
|
|
- } else {
|
|
|
- try {
|
|
|
- if (part.getFileName() == null) {
|
|
|
- return emailContentInfoDTO;
|
|
|
- }
|
|
|
- String fileName1 = MimeUtility.decodeText(part.getFileName());
|
|
|
- if (isSupportedFileType(fileName1)) {
|
|
|
- return emailContentInfoDTO;
|
|
|
- }
|
|
|
- emailContentInfoDTO.setFileName(fileName1);
|
|
|
- String realPath = filePath + fileName1;
|
|
|
- File saveFile = new File(realPath);
|
|
|
- if (!saveFile.exists()) {
|
|
|
- if (!saveFile.getParentFile().exists()) {
|
|
|
- saveFile.getParentFile().mkdirs();
|
|
|
- }
|
|
|
- FileUtil.saveFile(saveFile, part);
|
|
|
- } else {
|
|
|
- cn.hutool.core.io.FileUtil.del(saveFile);
|
|
|
- FileUtil.saveFile(saveFile, part);
|
|
|
- }
|
|
|
- emailContentInfoDTO.setFilePath(realPath);
|
|
|
- } catch (Exception e) {
|
|
|
- return emailContentInfoDTO;
|
|
|
- }
|
|
|
- }
|
|
|
- } catch (MessagingException e) {
|
|
|
- logger.info("邮件正文采集失败 -> 文件名:{}, 报错堆栈:{}", fileName, ExceptionUtil.stacktraceToString(e));
|
|
|
- return emailContentInfoDTO;
|
|
|
- }
|
|
|
- return emailContentInfoDTO;
|
|
|
- }
|
|
|
+// /**
|
|
|
+// * 采集邮件正文
|
|
|
+// *
|
|
|
+// * @param part 邮件消息体
|
|
|
+// * @param partContent 邮件消息内筒
|
|
|
+// * @param filePath 文件路径
|
|
|
+// * @param fileName 文件名
|
|
|
+// * @return 采集到邮件正文(html格式包含table标签)
|
|
|
+// */
|
|
|
+// public static EmailContentInfoDTO collectTextPart(MimeBodyPart part, String partContent, String filePath, String fileName) {
|
|
|
+// EmailContentInfoDTO emailContentInfoDTO = new EmailContentInfoDTO();
|
|
|
+// try {
|
|
|
+// if ((part.getContentType().contains("text/html") || part.getContentType().contains("TEXT/HTML"))) {
|
|
|
+// emailContentInfoDTO.setEmailContent(partContent);
|
|
|
+// String savePath = filePath + fileName;
|
|
|
+// File saveFile = new File(savePath);
|
|
|
+// if (!saveFile.exists()) {
|
|
|
+// if (!saveFile.getParentFile().exists()) {
|
|
|
+// saveFile.getParentFile().mkdirs();
|
|
|
+// saveFile.getParentFile().setExecutable(true);
|
|
|
+// }
|
|
|
+// }
|
|
|
+// //获取邮件编码
|
|
|
+// String contentType = part.getContentType();
|
|
|
+// String html = partContent.toString();
|
|
|
+// try {
|
|
|
+// if (contentType.contains("charset=")) {
|
|
|
+// contentType = contentType.substring(contentType.indexOf("charset=") + 8).replaceAll("\"", "");
|
|
|
+// html = html.replace("charset=" + contentType.toLowerCase(), "charset=UTF-8");
|
|
|
+// html = html.replace("charset=" + contentType.toUpperCase(), "charset=UTF-8");
|
|
|
+// }
|
|
|
+// if (savePath.contains(":")) {
|
|
|
+// savePath = savePath.replaceAll(":", "");
|
|
|
+// }
|
|
|
+// cn.hutool.core.io.FileUtil.writeUtf8String(html, new File(savePath));
|
|
|
+// } catch (Exception e) {
|
|
|
+// logger.error(e.getMessage(), e);
|
|
|
+// }
|
|
|
+// emailContentInfoDTO.setFileName(fileName);
|
|
|
+// emailContentInfoDTO.setFilePath(savePath);
|
|
|
+// } else {
|
|
|
+// try {
|
|
|
+// if (part.getFileName() == null) {
|
|
|
+// return emailContentInfoDTO;
|
|
|
+// }
|
|
|
+// String fileName1 = MimeUtility.decodeText(part.getFileName());
|
|
|
+// if (isSupportedFileType(fileName1)) {
|
|
|
+// return emailContentInfoDTO;
|
|
|
+// }
|
|
|
+// emailContentInfoDTO.setFileName(fileName1);
|
|
|
+// String realPath = filePath + fileName1;
|
|
|
+// File saveFile = new File(realPath);
|
|
|
+// if (!saveFile.exists()) {
|
|
|
+// if (!saveFile.getParentFile().exists()) {
|
|
|
+// saveFile.getParentFile().mkdirs();
|
|
|
+// }
|
|
|
+// FileUtil.saveFile(saveFile, part);
|
|
|
+// } else {
|
|
|
+// cn.hutool.core.io.FileUtil.del(saveFile);
|
|
|
+// FileUtil.saveFile(saveFile, part);
|
|
|
+// }
|
|
|
+// emailContentInfoDTO.setFilePath(realPath);
|
|
|
+// } catch (Exception e) {
|
|
|
+// return emailContentInfoDTO;
|
|
|
+// }
|
|
|
+// }
|
|
|
+// } catch (MessagingException e) {
|
|
|
+// logger.info("邮件正文采集失败 -> 文件名:{}, 报错堆栈:{}", fileName, ExceptionUtil.stacktraceToString(e));
|
|
|
+// return emailContentInfoDTO;
|
|
|
+// }
|
|
|
+// return emailContentInfoDTO;
|
|
|
+// }
|
|
|
|
|
|
/**
|
|
|
* 判断邮件是否符合解析条件
|
|
@@ -306,6 +294,8 @@ public class EmailUtil {
|
|
|
props.put("mail.imap.starttls.enable", "true");
|
|
|
props.put("mail.imap.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
|
|
|
props.put("mail.imap.socketFactory.fallback", "false");
|
|
|
+// // 设置数据块大小为 64k(可调整)
|
|
|
+// props.put("mail.imap.fetchsize", "65536");
|
|
|
}
|
|
|
return props;
|
|
|
}
|