How to retrieve gmail sub-folders/labels using POP3?
Asked Answered
J

1

2

The code below uses javamail API to access gmail,

    String host = "pop.gmail.com";
    int port = 995;
    Properties properties = new Properties();
    properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

    final javax.mail.Session session = javax.mail.Session.getInstance(properties);
    store = session.getStore("pop3s");
    store.connect(host, port, mCredentialaNme, mCredentialApss);

// ***************************************************************
Folder personalFolders[] = store.getDefaultFolder().list( "*" );
    // ***************************************************************
    for (Folder object : personalFolders) {
        // ***********************************
        System.out.println( object.list("*") );    
        // ***********************************
        if ((object.getType() & javax.mail.Folder.HOLDS_MESSAGES) != 0){
            object.open(Folder.READ_ONLY);
            Message messes[] = object.getMessages();

            System.out.println(object.getFullName());
            System.out.println("====================");
            for (Message object1 : messes) {
                System.out.println(object1.getFrom() + " - " + object1.getSubject());
            }
            object.close(false);
        }
    }

if (store.isConnected()) {
        store.close();
    }

The trouble is that this code only lists the INBOX folder whereas there are no less than 20 labels defined. What should be done to get the code to list/access these nested folders/labels?

Jefferyjeffie answered 8/5, 2011 at 6:20 Comment(0)
M
5

Don't use POP, use IMAP if you want labels/folders.

As noted in the javamail docs, due to the nature of the POP protocol, a POP message store always

Contains only one folder, "INBOX".

Margay answered 8/5, 2011 at 6:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.