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?
Here is the code that works. This will give you handle to all the Labels. To go deeper in a
folder
, you may performfolder.list()
or you can usestore.getDefaultFolder().list("*")
to retrieve all the folders and sub-folders as suggested in the other answer.Output:
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
How about
store.getDefaultFolder().list()
? Just a guess, though.Sergey is close, but by default JavaMail's
list()
does aLIST "" %
, 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 aLIST "" *
instead. Otherwise, you'll just get backINBOX
,[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]
):You can access other folders like this
etc.