【问题标题】:Java Mail Api not returning all mailsJava Mail Api 未返回所有邮件
【发布时间】:2017-08-29 06:26:47
【问题描述】:

您好,我正在尝试获取 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. 我还想只获取主要标签邮件。如何在我的代码中应用标签级过滤器?

【问题讨论】:

  • 主标签?如果您在谈论 gmail,请用标签指定它。
  • @Clijsters:是的,我正在尝试使用 gmail。

标签: java gmail jakarta-mail


【解决方案1】:

取自here

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

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

关于 POP3:

POP3 是用于访问单个邮箱的非常有限的协议。它的能力远不如IMAP

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-11
      • 1970-01-01
      • 2011-01-08
      • 1970-01-01
      • 1970-01-01
      • 2019-02-02
      • 2015-06-14
      相关资源
      最近更新 更多