开发者

Can't get emails in Drafts, Spam and Trash folders from google using javax.mail

I want to get emails from all folders from gmail. From inbox, sent and other folders I receive emails successfully. But when I try to get it from Drafts, Spam and Trash I get an exception:

09:51:45,622 ERROR MailRetriever.[main]getNoFlaggedMails:142 - Can't get messages: javax.mail.MessagingException: connection failure
javax.mail.MessagingException: connection failure
at com.sun.mail.imap.IMAPStore.getProtocol(IMAPStore.java:742)
at com.sun.mail.imap.IMAPFolder.o开发者_如何学Gopen(IMAPFolder.java:910)
at ua.com.stormlabs.gap.gmail.MailRetriever.getNoFlaggedMails(MailRetriever.java:133)
at ua.com.stormlabs.gap.gmail.GMailServiceProcessor.processFolder(GMailServiceProcessor.java:95)
at ua.com.stormlabs.gap.gmail.GMailServiceProcessor.start(GMailServiceProcessor.java:80)
at ua.com.stormlabs.gap.gmail.GMailGapApp.main(GMailGapApp.java:21)

This is code for retrieving mails:

Folder folder = imapSslStore.getFolder(folderName);
folder.open(Folder.READ_WRITE);
openedFolders.put(folderName, folder);

Flags searchFlags = new Flags(Flags.Flag.USER);
searchFlags.add(FLAG_PREFIX + READ_BY_GAP_FLAG);
Message[] messages = folder.search(new FlagTerm(searchFlags, false));
log.debug("Messages list retrieved: " + messages.length);
return messages;

Folder names I try to open unsuccessfully:

[Gmail]/Drafts
[Gmail]/Spam
[Gmail]/Trash


I have found in my Gmail account the trash folder is named "[Google Mail]/Bin" etc. and not "Gmail]/Trash".

I am in the UK and when I first opened my Gmail account it was indeed me@googlemail.com not me @gmail.com.

Hope this helps.


The simplest answer is that Draft, Spam and Trash are not folders but Labels in Google Mail. I am doing a Google search to validate my answer.


Update: After some Google search, I have found a SO Post which states that the [Gmail]/* folders are non-selectable folders.


Folder[] f = store.getDefaultFolder().list();
for(Folder fd:f){
    Folder t[]=fd.list();

      System.out.println("-------"+fd.getName()+"------");
    for(Folder f1:t)
        System.out.println("->"+f1.getName());

}


The following folder name :

[Gmail]/Spam

works perfectly for me. Here is my code I often use to read the spam folder using Javamail :

private void readFolder(int max, boolean deletes, List<Message> result, Store store, String folderName) throws MessagingException, IOException {
    Folder folder = null;
    try {
        folder = store.getFolder(folderName);
        folder.open(Folder.READ_WRITE);
        Message[] messages = folder.getMessages();
        for (int i = 0; i < messages.length && result.size() < max; i++) {
            Message message = messages[i];
            if (deletes && (!message.getFlags().contains(Flag.DELETED))) {
                message.setFlag(Flag.DELETED, true);
            }
            result.add(message);
        }
    } finally {
        if (folder != null) {
            try {
                folder.close(true);
            } catch (Exception e) {
            }
        }
    }
}


Below listed are some of the default GMAIL folder names.

INBOX
Notes
Personal
Receipts
Work
[Gmail]/All Mail
[Gmail]/Drafts
[Gmail]/Important
[Gmail]/Sent Mail
[Gmail]/Spam
[Gmail]/Starred
[Gmail]/Trash

Code ex:
Folder mailFolder = store.getFolder("[Gmail]/Trash");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜