Search code examples
javagmailimapapache-commons-net

Java: IMAP with Apache Commons Net - select root folder


I am using Apache Commons Net and my goal is to get the list of existing Gmail folders. I'm trying the following code:

IMAPClient imap = new IMAPSClient();
imap.setDefaultPort(port);
imap.addProtocolCommandListener(new PrintCommandListener(System.out, true));
imap.connect(server);
imap.login(username, password);
imap.list("", "*")

But it gives me:

I/System.out: * OK Gimap ready for requests from 5.18.55.93 fi8mb55620436ldc
I/System.out: AAAA LOGIN *******
I/System.out: * CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 UIDPLUS COMPRESS=DEFLATE ENABLE MOVE CONDSTORE ESEARCH UTF8=ACCEPT LIST-EXTENDED LIST-STATUS LITERAL- APPENDLIMIT=35651584
I/System.out: AAAA OK [email protected] authenticated (Success)
I/System.out: AAAB LIST  *
I/System.out: AAAB BAD Could not parse command

I need the server to provide me with a list of folders because custom or localized ones might exist.


Solution

  • Actually this

    I/System.out: AAAB LIST  *
    

    gave me an idea to replace

    imap.list("", "*")
    

    with

    imap.list("\"\"", "\"*\"");
    

    which worked like charm.