As Bill Shannon said, You can use Gmail attributes to get special folders like Trash. But this will work only with Gmail.
javax.mail.Folder[] folders = store.getDefaultFolder().list("*");
If you print this, it should look like the following as per Gmail
a004 LIST "" "*"
* LIST (\HasNoChildren) "/" "INBOX"
* LIST (\Noselect \HasChildren) "/" "[Gmail]"
* LIST (\HasNoChildren \All) "/" "[Gmail]/All Mail"
* LIST (\HasNoChildren \Drafts) "/" "[Gmail]/Drafts"
* LIST (\HasNoChildren \Important) "/" "[Gmail]/Important"
* LIST (\HasNoChildren \Sent) "/" "[Gmail]/Sent Mail"
* LIST (\HasNoChildren \Junk) "/" "[Gmail]/Spam"
* LIST (\HasNoChildren \Flagged) "/" "[Gmail]/Starred"
* LIST (\HasNoChildren \Trash) "/" "[Gmail]/Trash"
a004 OK Success
Once you have the folders with you, you can iterate for the attribute you are looking for.
For [Gmail]/All Mail
, mailFolder = "\\All"
. similarly for [Gmail]/Trash
it will be mailFolder = "\\Trash"
private static IMAPFolder getLocalisedFolder(IMAPStore store, String mailFolder) throws MessagingException {
Folder[] folders = store.getDefaultFolder().list("*");
for (Folder folder : folders) {
IMAPFolder imapFolder = (IMAPFolder) folder;
for (String attribute : imapFolder.getAttributes()) {
if (mailFolder.equals(attribute)) {
return imapFolder;
}
}
}
return null;
}