This commit is contained in:
oneao committed 2025-07-05 17:25:30 +08:00
1 parent 417834bd13
commit 9454e48bab
2 files changed
+66 -28

No files matched your search

@@ -1,38 +1,82 @@
package com.email;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.Properties;
import javax.mail.internet.MimeBodyPart;
import javax.mail.search.FlagTerm;
import java.io.*;
import java.util.*;
import java.util.concurrent.*;
public class FolderHierarchyTest {
private final ExecutorService executor = Executors.newFixedThreadPool(8);
public static void main(String[] args) {
String host = "imap.qq.com"; // IMAP 服务器地址
String username = "717406575@qq.com"; // 邮箱地址
String password = "dhqesrwzgblrbcac"; // 邮箱密码
public static void main(String[] args) throws Exception {
new FolderHierarchyTest().run();
}
public void run() throws Exception {
Properties props = new Properties();
props.put("mail.store.protocol", "imap");
props.put("mail.imap.ssl.enable", "true"); // 使用 SSL
props.put("mail.store.protocol", "imaps");
try {
Session session = Session.getDefaultInstance(props);
Store store = session.getStore("imap");
store.connect(host, username, password);
Session session = Session.getInstance(props, null);
Store store = session.getStore();
store.connect("imap.qq.com", "717406575@qq.com", "dhqesrwzgblrbcac");
Folder defaultFolder = store.getDefaultFolder();
printFolderHierarchy(defaultFolder, "");
Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_ONLY);
store.close();
} catch (Exception e) {
e.printStackTrace();
if (!(inbox instanceof UIDFolder)) {
throw new RuntimeException("This IMAP server does not support UIDFolder");
}
UIDFolder uidFolder = (UIDFolder) inbox;
Message[] messages = inbox.search(new FlagTerm(new Flags(Flags.Flag.SEEN), false));
int batchSize = 50;
for (int i = 0; i < messages.length; i += batchSize) {
int start = i;
int end = Math.min(i + batchSize, messages.length);
executor.submit(() -> processBatch(Arrays.copyOfRange(messages, start, end), uidFolder));
}
executor.shutdown();
executor.awaitTermination(30, TimeUnit.MINUTES);
inbox.close(false);
store.close();
}
private void processBatch(Message[] batch, UIDFolder uidFolder) {
for (Message msg : batch) {
try {
long uid = uidFolder.getUID(msg);
String subject = msg.getSubject();
Address[] froms = msg.getFrom();
System.out.println("UID: " + uid + " Subject: " + subject);
Object content = msg.getContent();
if (content instanceof Multipart) {
Multipart mp = (Multipart) content;
for (int i = 0; i < mp.getCount(); i++) {
BodyPart bp = mp.getBodyPart(i);
if (Part.ATTACHMENT.equalsIgnoreCase(bp.getDisposition())) {
saveAttachment((MimeBodyPart) bp, uid);
}
}
}
} catch (Exception e) {
System.err.println("Failed to process message: " + e.getMessage());
}
}
}
private static void printFolderHierarchy(Folder folder, String indent) throws MessagingException {
Folder[] subFolders = folder.list();
for (Folder f : subFolders) {
System.out.println(f.getFullName());
}
private void saveAttachment(MimeBodyPart part, long uid) throws Exception {
String fileName = part.getFileName();
File dir = new File("attachments/" + uid);
if (!dir.exists()) dir.mkdirs();
File file = new File(dir, fileName);
part.saveFile(file);
System.out.println("Saved attachment: " + file.getAbsolutePath());
}
}