IMAP List extension JAVA
Asked Answered
V

5

6

I'm building a tool that need access to mail specific folders (e.g. '[Gmail]/Trash', '[Gmail]/Sent'). It seems that the names are localized with respect to the user localization settings, so '[Gmail]/Trash' show as '[Gmail]/Papelera' to Spanish users for example.

I read about XLIST command but now is deprecated in favor of the IMAP LIST Extension (https://developers.google.com/gmail/imap_extensions#special-use_extension_of_the_list_command).

I tried to do it that way javax.mail.Folder.list("\Trash") but nothing is returned.

How can I use the IMAP List extension in JAVA?

PS: Using several email providers, not just Gmail.

Veiling answered 29/1, 2014 at 10:11 Comment(1)
It is possible that the Java does not support or parse the extension fields. Can you somehow manually issue the command? Or fork the javamail source to add the support you need?Iconoduly
S
4

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;
}
Salientian answered 27/10, 2014 at 16:1 Comment(0)
S
0

This should help:

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", "[email protected]", "mypassword");
        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("foldername->"+folder.getFullName() + " folder msg count->" + folder.getMessageCount());
            }
        }
    } catch (MessagingException e) {
        e.printStackTrace();
    }
    catch (Exception e) {
        e.printStackTrace();
    }
Snore answered 29/1, 2014 at 10:39 Comment(4)
Thanks, but that's for explore folders, it's not a solution for a Trash independent language identification.Veiling
Not sure if i understood you. What do you mean by Trash independent language.Snore
In the "old code" I did iterating to find the trash as you have given me, but now I have multilanguage support and gmail trash's name changes according to the language settings of the user, not enough for find trash because can be called differently.Veiling
As I say above I found the solution is using IMAP List extension but I can't apply it.Veiling
K
0

You say you tried this:

javax.mail.Folder.list("\Trash")

Try without the slash:

javax.mail.Folder.list("Trash")

See how it goes. Now that's assuming that the folder is actually called "Trash". If it's localised in some other language, then you probably need to list all folders ("*"), iterate through them one by one, and find the one that gave you the \Trash attribute. I'm not very familiar with JavaMail so I don't know whether/how it gives you back the folder attributes.

Kennethkennett answered 29/1, 2014 at 12:23 Comment(0)
C
0

maybe you can check (once you have a Folder folder) if

if ((folder.getType() & Folder.HOLDS_MESSAGES) != 0) {

and then check if messages in the folder have this flag:

Flags.Flag.DELETED

if so, that would mean this is the Trash? I don't remmeber if when a msg is deleted it is moved to the Trash or it can remain in the folder...if it is moved, that could make the trick.

Casmey answered 29/1, 2014 at 12:32 Comment(1)
Messages in the Trash folder are not normally marked \Deleted. That's a just a transitional flag before they're expunged.Iconoduly
K
0

Gmail no longer requires the use of the XLIST command. Gmail returns attributes with the regular IMAP LIST command that indicate the use of the localized mailboxes. You can access these attributes using the IMAPFolder.getAttributes method.

I'm afraid that doesn't help you if you have another IMAP server that only returns this information with XLIST.

Kamerun answered 30/1, 2014 at 4:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.