Question

I am trying to get SENT Folder in Gmail. But it's depend on language. Ex, If I used English My tree list below:

[Gmail]/All Mail
[Gmail]/Bin
[Gmail]/Drafts
....

But If I use other language, All folder will be translated to this language. I try to create index for folder but It's not successful because All folder will be sorted follow alphabet. I can't hardcode like this : getFolder("[Gmail]/Sent Mail"), in other language "Sent Mail will be translated. I have a question. I can get all name of folder but how to recognize where is "SENT MAIL" folder to get all mail were sent.

Was it helpful?

Solution

Gmail folders have attributes and you can rely on them to get the folder that you want (if it's one of the standard ones).

So, if you do

store.getDefaultFolder().list("*");

You would get all the folders, not just the first level. And response from the server should looks like this:

A3 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"
* LIST (\HasNoChildren) "/" "[Gmail]Trash"

Once you get the the list of folders you can iterate them looking for the attribute that you want. So if you are looking for the sent folder you could do this:

public Folder getSentFolder(Folder[] folders) throws MessagingException {
    for (Folder folder : folders) {
        IMAPFolder imapFolder = (IMAPFolder) folder;
        for(String attribute : imapFolder.getAttributes()) {
            if ("\\Sent".equals(attribute)) {
                return folder;
            }
        }
    }
    return null;
}

And then you have a folder selector that is language agnostic.

NOTE That this works because is Gmail. Other email servers may not have the attributes

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top