Java Mail Api没有返回所有邮件

时间:2017-08-29 06:26:47

标签: java gmail javamail

您好我正在尝试收集Gmail收件箱的所有邮件,但它并未返回所有邮件。它返回的邮件太少了。不是最新的。我正在使用以下代码进行测试:

public class ReadEmail {

    public static void check(String host, String storeType, final String user, final String password) {
        try {

            // create properties field
            Properties properties = new Properties();

            properties.put("mail.pop3.host", host);
            properties.put("mail.pop3.port", "995");
            properties.put("mail.pop3.starttls.enable", "true");
            Session emailSession = Session.getDefaultInstance(properties);
            emailSession.setDebug(true);

            // create the POP3 store object and connect with the pop server
            Store store = emailSession.getStore("pop3s");

            store.connect(host, user, password);

            // create the folder object and open it
            Folder emailFolder = store.getFolder("Inbox");
            emailFolder.open(Folder.READ_ONLY);

            // retrieve the messages from the folder in an array and print it
            Message[] messages = emailFolder.getMessages();
            System.out.println("messages.length---" + messages.length);

            // close the store and folder objects
            emailFolder.close(false);
            store.close();

        } catch (NoSuchProviderException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {

        String host = "pop.gmail.com";
        String mailStoreType = "pop3";
        String username = "****@gmail.com";// change accordingly
        String password = "****";// change accordingly

        check(host, mailStoreType, username, password);

    }
}
  1. 我的代码中没有出错。我已经完成了给定here的其他设置。

  2. 我还想获得主要标签邮件。如何在我的代码中应用标签级过滤器?

2 个答案:

答案 0 :(得分:0)

使用IMAP服务器而不是使用POP服务器后,它工作正常。我不知道原因,但IMAP服务器工作正常。

答案 1 :(得分:0)

取自here

  

问:为什么在使用POP3访问Gmail时,我看不到所有邮件?

     

答:Gmail具有控制哪些消息可用的设置   通过POP3协议。请参阅Gmail设置页面以更改   配置您的Gmail帐户。

关于POP3:

  

POP3是一种访问单个邮箱的非常有限的协议。它的能力远低于IMAP