47

I am using JavaMail API to connect to my personal account. I have list of folders (labels) in my Gmail account which I created + the default folders like Inbox, Drafts etc. How can I list all the available folders (the default and the user created)?

I can access the particular folder using this API: Folder inbox = store.getFolder("Inbox");. Is there any other API to get the list of folders available in a mail account?

bluish
  • 26,356
  • 27
  • 122
  • 180
Jagadesh
  • 6,489
  • 8
  • 29
  • 30
  • 1
    Why on earth did you accept an answer that didn't give you the results you were looking for? – dkarp Jan 26 '11 at 05:51

6 Answers6

71

Sergey is close, but by default JavaMail's list() does a LIST "" %, which gives you only top-level folders. GMail puts its system folders (All Mail, Drafts, Sent Mail, Spam, Starred, and Trash) under the non-selectable folder [Gmail], so you really need to do a LIST "" * instead. Otherwise, you'll just get back INBOX, [Gmail], and your labels.

Here's some sample code that connects to GMail, fetches the folder list, and prints out the name and message count for each non-\NoSelect folder (i.e. the ones that aren't just hierarchy placeholders, like [Gmail]):

Properties props = System.getProperties();
props.setProperty("mail.store.protocol", "imaps");
try {
    Session session = Session.getDefaultInstance(props, null);
    javax.mail.Store store = session.getStore("imaps");
    store.connect("imap.gmail.com", "<username>@gmail.com", "<password>");
    javax.mail.Folder[] folders = store.getDefaultFolder().list("*");
    for (javax.mail.Folder folder : folders) {
        if ((folder.getType() & javax.mail.Folder.HOLDS_MESSAGES) != 0) {
            System.out.println(folder.getFullName() + ": " + folder.getMessageCount());
        }
    }
} catch (MessagingException e) {
    e.printStackTrace();
}
bluish
  • 26,356
  • 27
  • 122
  • 180
dkarp
  • 14,483
  • 6
  • 58
  • 65
48

Here is the code that works. This will give you handle to all the Labels. To go deeper in a folder, you may perform folder.list() or you can use store.getDefaultFolder().list("*") to retrieve all the folders and sub-folders as suggested in the other answer.

Properties props = System.getProperties();
props.setProperty("mail.store.protocol", "imaps");
Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore("imaps");
store.connect("imap.gmail.com", "YOURMAILID@gmail.com", "UR_P@ZZWRD");
System.out.println(store);

Folder[] f = store.getDefaultFolder().list();
for(Folder fd:f)
    System.out.println(">> "+fd.getName());

Output:

>> INBOX
>> Personal
>> Receipts
>> Travel
>> Work
>> [Gmail]


OLD ANSWER

Please note this is not correct, it's rightly pointed in this answer by dkarp

These should do:

http://java.sun.com/products/javamail/javadocs/javax/mail/Store.html#getSharedNamespaces%28%29

http://java.sun.com/products/javamail/javadocs/javax/mail/Store.html#getUserNamespaces%28java.lang.String%29

Community
  • 1
  • 1
Nishant
  • 54,584
  • 13
  • 112
  • 127
  • `Folder[] folders= store.getPersonalNamespaces();` when i tried this code the folders.length is always 1. But i have 8 folders in my gmail account .. Similarly if i tried with `Folder[] folders= store.getSharedNamespaces();` the length is always zero. – Jagadesh Jan 25 '11 at 07:19
  • This answer is simply wrong. This code causes JavaMail to issue [the `NAMESPACE` command](http://tools.ietf.org/html/rfc2342#section-5), which aids in discovery of prefixes for the authenticated user's folders, other users' folders, and shared folders. But it certainly doesn't do a folder listing. – dkarp Jan 26 '11 at 05:52
  • 2
    The code you've included doesn't give the output you listed. You need the explicit `"*"` in the `list()`, otherwise it will return only `INBOX` and the non-selectable `[Gmail]`. – dkarp Jan 26 '11 at 14:01
  • @dkarp weird, I see that output with the code. I just ran that code and pasted the output and I haven't used any "*". If I do not use the "*" I get all of my Label, I have just created a new Label and I can see that appearing without (*). THis: >> Expenses >> INBOX >> Personal >> Receipts >> Travel >> Work >> [Gmail]. With "*", however, I see all the labels + Draft, sent, all-mails as well. – Nishant Jan 26 '11 at 14:04
  • It doesn't shows the sub folder of "Inbox". I have inbox which contains "mail1","mail2" I have use your code to get folder list but it only shows inbox not its subfolders. – Mister X Sep 24 '16 at 12:46
8

You can access other folders like this

store.getFolder("[Gmail]/Sent Mail");
store.getFolder("[Gmail]/Drafts");

etc.

stealthyninja
  • 10,343
  • 11
  • 51
  • 59
user889386
  • 89
  • 1
  • 1
0

How about store.getDefaultFolder().list()? Just a guess, though.

bluish
  • 26,356
  • 27
  • 122
  • 180
Sergei Tachenov
  • 24,345
  • 8
  • 57
  • 73
0

You can try this:

Folder[] folderList = store.getDefaultFolder().list("*");
Dino
  • 7,779
  • 12
  • 46
  • 85
Mehdi
  • 3,795
  • 3
  • 36
  • 65
-1

You can right-click on different buttons on the side panel of your mail box and inspect the names for different folders that are used in different mails.

This works for every email client. The list command works but I felt this makes the process quicker.

Sardaar54
  • 1
  • 3