u
This commit is contained in:
1 parent
417834bd13
commit
9454e48bab
2 files changed
+66
-28
No files matched your search
@@ -1,38 +1,82 @@
|
|||||||
package com.email;
|
package com.email;
|
||||||
|
|
||||||
|
|
||||||
import javax.mail.*;
|
import javax.mail.*;
|
||||||
import javax.mail.internet.*;
|
import javax.mail.internet.MimeBodyPart;
|
||||||
import java.util.Properties;
|
import javax.mail.search.FlagTerm;
|
||||||
|
import java.io.*;
|
||||||
|
import java.util.*;
|
||||||
|
import java.util.concurrent.*;
|
||||||
|
|
||||||
public class FolderHierarchyTest {
|
public class FolderHierarchyTest {
|
||||||
|
private final ExecutorService executor = Executors.newFixedThreadPool(8);
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) throws Exception {
|
||||||
String host = "imap.qq.com"; // IMAP 服务器地址
|
new FolderHierarchyTest().run();
|
||||||
String username = "717406575@qq.com"; // 邮箱地址
|
}
|
||||||
String password = "dhqesrwzgblrbcac"; // 邮箱密码
|
|
||||||
|
|
||||||
|
public void run() throws Exception {
|
||||||
Properties props = new Properties();
|
Properties props = new Properties();
|
||||||
props.put("mail.store.protocol", "imap");
|
props.put("mail.store.protocol", "imaps");
|
||||||
props.put("mail.imap.ssl.enable", "true"); // 使用 SSL
|
|
||||||
|
|
||||||
try {
|
Session session = Session.getInstance(props, null);
|
||||||
Session session = Session.getDefaultInstance(props);
|
Store store = session.getStore();
|
||||||
Store store = session.getStore("imap");
|
store.connect("imap.qq.com", "717406575@qq.com", "dhqesrwzgblrbcac");
|
||||||
store.connect(host, username, password);
|
|
||||||
|
|
||||||
Folder defaultFolder = store.getDefaultFolder();
|
Folder inbox = store.getFolder("INBOX");
|
||||||
printFolderHierarchy(defaultFolder, "");
|
inbox.open(Folder.READ_ONLY);
|
||||||
|
|
||||||
store.close();
|
if (!(inbox instanceof UIDFolder)) {
|
||||||
} catch (Exception e) {
|
throw new RuntimeException("This IMAP server does not support UIDFolder");
|
||||||
e.printStackTrace();
|
}
|
||||||
|
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 {
|
private void saveAttachment(MimeBodyPart part, long uid) throws Exception {
|
||||||
Folder[] subFolders = folder.list();
|
String fileName = part.getFileName();
|
||||||
for (Folder f : subFolders) {
|
File dir = new File("attachments/" + uid);
|
||||||
System.out.println(f.getFullName());
|
if (!dir.exists()) dir.mkdirs();
|
||||||
}
|
|
||||||
|
File file = new File(dir, fileName);
|
||||||
|
part.saveFile(file);
|
||||||
|
System.out.println("Saved attachment: " + file.getAbsolutePath());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3,12 +3,6 @@
|
|||||||
- github:https://github.com/PaddlePaddle/PaddleNLP/tree/develop
|
- github:https://github.com/PaddlePaddle/PaddleNLP/tree/develop
|
||||||
- 官网:
|
- 官网:
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# 报错
|
# 报错
|
||||||
|
|
||||||
## AttributeError: module 'h11' has no attribute 'Event'. Did you mean: '_events'?
|
## AttributeError: module 'h11' has no attribute 'Event'. Did you mean: '_events'?
|
||||||
|
|||||||
Reference in new issue
Block a user