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

本秂侑毒 提交于 2019-12-30 11:06:39

问题


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?


回答1:


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".



来源:https://stackoverflow.com/questions/5925944/how-to-retrieve-gmail-sub-folders-labels-using-pop3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!