wangzaijun пре 3 недеља
родитељ
комит
c06fb79635

+ 22 - 4
mo-daq/src/main/java/com/smppw/modaq/domain/service/EmailParseService.java

@@ -37,9 +37,7 @@ import com.smppw.modaq.infrastructure.util.ArchiveUtil;
 import com.smppw.modaq.infrastructure.util.PdfUtil;
 import jakarta.mail.*;
 import jakarta.mail.internet.MimeUtility;
-import jakarta.mail.search.ComparisonTerm;
-import jakarta.mail.search.ReceivedDateTerm;
-import jakarta.mail.search.SearchTerm;
+import jakarta.mail.search.*;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Value;
@@ -701,7 +699,7 @@ public class EmailParseService {
                                                                   Store store, String folderName) throws MessagingException {
         // 默认读取收件箱的邮件
         Folder folder = store.getFolder(folderName);
-        folder.open(Folder.READ_ONLY);
+        folder.open(Folder.READ_WRITE);
         Message[] messages = getEmailMessage(folder, mailboxInfoDTO.getProtocol(), startDate);
         if (messages == null || messages.length == 0) {
             log.warn("{} 获取不到邮件 -> 邮箱信息:{},开始时间:{},结束时间:{}", folderName, mailboxInfoDTO, startDate, endDate);
@@ -715,6 +713,10 @@ public class EmailParseService {
             Integer emailType;
             String senderEmail;
             String emailTitle = message.getSubject();
+            if (isMessageRead(message)) {
+                log.warn("{} 邮件{} 已读,不用重新下载解析!", folderName, emailTitle);
+                continue;
+            }
             try {
                 Date emailDate = message.getSentDate();
                 String emailDateStr = DateUtil.format(emailDate, DateConst.YYYY_MM_DD_HH_MM_SS);
@@ -765,6 +767,8 @@ public class EmailParseService {
                 log.error("{} 获取邮箱的邮件{} 报错,堆栈信息:{}", folderName, emailTitle, ExceptionUtil.stacktraceToString(e));
             }
         }
+        // 设置已读标志
+        folder.setFlags(messages, new Flags(Flags.Flag.SEEN), true);
         folder.close(false);
         return emailMessageMap;
     }
@@ -883,4 +887,18 @@ public class EmailParseService {
             throw new RuntimeException(e);
         }
     }
+
+    /**
+     * 检查邮件是否已读
+     * @param message 邮件对象
+     * @return true表示已读,false表示未读
+     * @throws MessagingException 如果访问邮件标志时出错
+     */
+    private boolean isMessageRead(Message message) throws MessagingException {
+        // 获取邮件的所有标志
+        Flags flags = message.getFlags();
+
+        // 检查是否包含 SEEN 标志
+        return flags.contains(Flags.Flag.SEEN);
+    }
 }

+ 2 - 2
mo-daq/src/test/java/com/smppw/modaq/MoDaqApplicationTests.java

@@ -37,9 +37,9 @@ public class MoDaqApplicationTests {
 
     @Test
     public void reportTest() {
-        MailboxInfoDTO emailInfoDTO = this.buildMailbox("*@simuwang.com", "**");
+        MailboxInfoDTO emailInfoDTO = this.buildMailbox("*@simuwang.com", "*");
         Date startDate = DateUtil.parse("2025-06-05 17:02:00", DateConst.YYYY_MM_DD_HH_MM_SS);
-        Date endDate = DateUtil.parse("2025-06-05 17:05:00", DateConst.YYYY_MM_DD_HH_MM_SS);
+        Date endDate = DateUtil.parse("2025-06-06 17:05:00", DateConst.YYYY_MM_DD_HH_MM_SS);
         try {
             List<String> folderNames = ListUtil.list(false);
 //            folderNames.add("其他文件夹/报告公告");