gpt4 book ai didi

java - Gmail JavaMail 邮件检索问题

转载 作者:行者123 更新时间:2023-12-02 08:31:41 25 4
gpt4 key购买 nike

我正在使用 JavaMail 开发一个项目。我想访问我的 Gmail 收件箱并获取邮件。我通过检查主题来查找特定消息。该消息有一个我保存的附件。该程序在第一次运行时运行良好。问题是,一旦我运行该程序,任何后续运行都看不到该消息。它不会显示为文件夹消息的一部分。如果我转到 gmail 帐户并设置“为所有邮件启用 POP(甚至是已下载的邮件)”(这是从一开始的设置),我可以在该消息再次停止出现在文件夹中之前再次看到该消息。我不明白,任何帮助都会很棒。

这是我获取消息的代码:

    Session session2 = Session.getDefaultInstance(props2, null);
Store store = session2.getStore("pop3s");
store.connect(getHost, username, password);
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
System.out.println(folder.getMessageCount());
Message messages[] = folder.getMessages();
for (Message message : messages) {
System.out.println(message.getSubject());
if (message.getSubject().equalsIgnoreCase("Input File")) {
if (message.getContent() instanceof Multipart) {
Multipart multipart = (Multipart) message.getContent();
for (int i = 0, n = multipart.getCount(); i < n; i++) {
Part part = multipart.getBodyPart(i);
String disposition = part.getDisposition();
if ((disposition != null) && ((disposition.equals(Part.ATTACHMENT) || (disposition.equals(Part.INLINE))))) {
File f = saveFile(part.getFileName(), part.getInputStream());
System.out.println(f.getPath());
}
}
}
}
}
folder.close(false);
store.close();
}

保存文件方法:

public static File saveFile(String filename, InputStream input) throws FileNotFoundException, IOException {
File file = new File(filename);
for (int i = 0; file.exists(); i++) {
file = new File(filename + i);
}
FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);

BufferedInputStream bis = new BufferedInputStream(input);
int aByte;
while ((aByte = bis.read()) != -1) {
bos.write(aByte);
}
bos.flush();
bos.close();
bis.close();
return file;
}

最佳答案

我尝试将代码切换为使用 imap,现在它似乎可以正常工作。我想我的问题与 pop3 和 gmail 有关。

关于java - Gmail JavaMail 邮件检索问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3215494/

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