我正在使用JavaMail进行项目工作。我想访问我的Gmail收件箱并获取消息。我通过查看主题寻找特定的消息。这条消息有我保存的附件。该程序在第一次运行时工作正常。问题是,一旦我运行该程序,任何后续运行都无法看到该消息。它不会显示为文件夹消息的一部分。如果我转到Gmail账户并设置“为所有邮件启用POP(甚至是已经下载的邮件)”(这是从一开始的设置),我可以再次看到该消息,然后再次停止出现在文件夹中。我不明白,任何帮助都会很棒。Gmail JavaMail邮件滞留问题
这里是我的代码,得到的消息:
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();
}
的saveFile的方法:
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;
}
查找替换部分
这个帖子: http://stackoverflow.com/questions/5366767/retrieve-未读电子邮件-gmail-javamail-api-imap – alibenmessaoud 2012-06-20 19:18:54