gpt4 book ai didi

java - 使用 JavaMail 加快电子邮件检索速度

转载 作者:行者123 更新时间:2023-12-02 04:07:41 29 4
gpt4 key购买 nike

目前,我的代码使用 IMAP (imaps) 和 JavaMail 访问我的 Gmail 收件箱,目的是按照从最新到最旧的顺序阅读发送给我自己的电子邮件,识别哪些邮件包含 .zip 或 .xap 格式的附件。如果找到,则会显示电子邮件的主题并询问我是否要下载附件。

如果我单击“否”,它会继续搜索。如果我单击"is",它会调用 createFolder 方法来创建一个目录,将附件保存在那里,然后提取它。

问题:我的收件箱中的最新电子邮件有一个 .zip 文件,因此可以很快找到它,但如果我单击“否”,则需要 20 秒以上才能找到下一封包含zip/xap 的原因有 2 个:(1) 最近有 20 多封电子邮件 (2) 最近有 2 封电子邮件包含附件,但不是 zip/xap。

我猜测这是由于在识别附件格式之前进行了递归来隔离附件,或者是由于其他一些冗余代码/方法造成的?我读过herehere获取配置文件可以提供帮助,而不是不必要地联系服务器。

我希望能够大幅减少延迟。 Fetch/Envelope 是正确的方法吗?我的代码是否困惑,或者您可以分享一个示例解决方案吗?谢谢!

完整代码:

public class ReceiveMailImap {
public ReceiveMailImap() {}

public static void doit() throws MessagingException, IOException {
Folder inbox = null;
Store store = null;

try {
Properties props = System.getProperties();
Session session = Session.getDefaultInstance(props, null);
//session.setDebug(true);
store = session.getStore("imaps");
store.connect("imap.gmail.com", "myAccount@gmail.com", "myPassword");
inbox = store.getFolder("Inbox");
inbox.open(Folder.READ_WRITE);
Message messages[] = inbox.getMessages();

// Loop newest to oldest
for (int i = messages.length - 1; i >= 0; i--) {
Message msg = messages[i];
//If current msg has no attachment, skip.
if (!hasAttachments(msg)) {
continue;
}

String from = "Sender Unknown";
if (msg.getReplyTo().length >= 1) {
from = msg.getReplyTo()[0].toString();
} else if (msg.getFrom().length >= 1) {
from = msg.getFrom()[0].toString();
}
subject = msg.getSubject();

if (from.contains("myAccount@gmail.com")) {
//Get My Documents

subject = subject.replaceAll("[^a-zA-Z0-9.-]", "_");

//Call Save Attachment -->>
saveAttachment(msg.getContent());
msg.setFlag(Flags.Flag.SEEN, true);
}
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
if (inbox != null) {
inbox.close(true);
}
if (store != null) {
store.close();
}
}
}

public static boolean hasAttachments(Message msg) throws MessagingException, IOException {
if (msg.isMimeType("multipart/mixed")) {
Multipart mp = (Multipart) msg.getContent();
if (mp.getCount() > 1) return true;
}
return false;
}

public static void saveAttachment(Object content)
throws IOException, MessagingException {
OutputStream out = null;
InputStream in = null;
try {
if (content instanceof Multipart) {
Multipart multi = ((Multipart) content);
int parts = multi.getCount();
for (int j = 0; j < parts; ++j) {
MimeBodyPart part = (MimeBodyPart) multi.getBodyPart(j);
if (part.getContent() instanceof Multipart) {
// part-within-a-part, do some recursion...
saveAttachment(part.getContent());
} else {
int allow = 0;

if (part.isMimeType("application/x-silverlight-app")) {
extension = "xap";
allow = 1;
} else {
if (part.isMimeType("application/zip")) {
extension = "zip";
allow = 1;
}
}
if (allow == 1) {
int dialogResult = JOptionPane.showConfirmDialog(null, "The most recent file is: " + subject + " Would you like to download this file?", "Question", 0);
if (dialogResult == JOptionPane.YES_OPTION) {
savePath = createFolder(subject) + "\\" + subject + "." + extension;
} else {
continue;
}
out = new FileOutputStream(new File(savePath)); in = part.getInputStream();
int k;
while ((k = in .read()) != -1) {
out.write(k);
}

//Unzip
savePath = savePath.toString();
Unzip unzipFile = new Unzip();
unzipFile.UnZipper(dir, savePath);
} else {
continue;
}
}
}
}
} finally {
if ( in != null) { in .close();
}
if (out != null) {
out.flush();
out.close();
}
}
}

public static File createFolder(String subject) {
JFileChooser fr = new JFileChooser();
FileSystemView myDocs = fr.getFileSystemView();
String myDocuments = myDocs.getDefaultDirectory().toString();
dir = new File(myDocuments + "\\" + subject);
savePathNoExtension = dir.toString();
dir.mkdir();
return dir;
}

public static void main(String args[]) throws Exception {
ReceiveMailImap.doit();
}
}

最佳答案

FetchProfile 允许您“批量”预取一些消息信息。稍后当您访问 Message 对象的相应字段时,将使用此预取信息。您可能想使用 FetchProfile.Item.CONTENT_INFOFetchProfile.Item.ENVELOPE 。除此之外,您的代码看起来不错。

关于java - 使用 JavaMail 加快电子邮件检索速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34098123/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com