2

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.open(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
Hleb
  • 295
  • 1
  • 4
  • 15

5 Answers5

5

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.

2
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());

}
2

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.

Community
  • 1
  • 1
Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
  • 2
    I use imapSslStore.getDefaultFolder().list("*"); to get all folders and i get such list: INBOX Personal Receipts Travel Work [Gmail] [Gmail]/All Mail [Gmail]/Drafts [Gmail]/Important [Gmail]/Sent Mail [Gmail]/Spam [Gmail]/Starred [Gmail]/Trash – Hleb Sep 05 '11 at 07:28
  • Then try doing `folderName = "Gmail/Drafts"` and see if it works (or `folderName = "/Drafts"`). – Buhake Sindi Sep 05 '11 at 07:34
  • No, now I get javax.mail.FolderNotFoundException. – Hleb Sep 05 '11 at 07:38
  • @Hleb, that doesn't help. What's the cause of the exception (and message)? – Buhake Sindi Sep 05 '11 at 07:38
  • 10:37:21,565 ERROR MailRetriever.[main]getNoFlaggedMails:142 - Can't get messages: javax.mail.FolderNotFoundException: /Drafts not found javax.mail.FolderNotFoundException: /Drafts not found at com.sun.mail.imap.IMAPFolder.checkExists(IMAPFolder.java:388) at com.sun.mail.imap.IMAPFolder.open(IMAPFolder.java:1000) at ua.com.stormlabs.gap.gmail.MailRetriever.getNoFlaggedMails(MailRetriever.java:133) at ua.com.stormlabs.gap.gmail.GMailServiceProcessor.processFolder(GMailServiceProcessor.java:95) – Hleb Sep 05 '11 at 07:42
  • Thank you, but it is strange, becouse I successfully get mails from [Gmail]/Important and [Gmail]/Starred. – Hleb Sep 05 '11 at 08:22
  • My guess is that the `Important` and `Starred` emails are labels from the `Inbox` message, i.e. there's a reference to an inbox message from the mentioned labels. – Buhake Sindi Sep 05 '11 at 08:28
  • Gmail]/Drafts is definitely selectable: C: eeeb2ce59e9340f4 SELECT "[Gmail]/Drafts" .... S: eeeb2ce59e9340f4 OK [READ-WRITE] [Gmail]/Drafts selected. (Success) – Pawel Lesnikowski Sep 05 '11 at 12:59
  • Those folders are selectable (I just tested). Check the exact IMAP command being sent; there is a chance that something should be quoted and isn't, or vice versa. – arnt Jan 26 '14 at 16:22
1

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) {
            }
        }
    }
}
Pascal Heraud
  • 365
  • 2
  • 8
0

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");
ChaseD20
  • 119
  • 1
  • 4