计算 Gmail 收件箱中的电子邮件数量

2022-01-23 00:00:00 gmail pop3 java jakarta-mail gmail-pop

这是计算 gmail 收件箱中邮件数量的代码.

This is the code that counts the number of mails in the gmail inbox.

Properties props = new Properties();
    props.put("mail.pop3.host" , "pop.gmail.com");
    props.put("mail.pop3.user" , "username");
    props.put("mail.pop3.socketFactory" , 995 );
    props.put("mail.pop3.socketFactory.class" , "javax.net.ssl.SSLSocketFactory" );
    props.put("mail.pop3.port" , 995);
    Session session = Session.getDefaultInstance(props , new Authenticator() {
        @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication( "username" , "password");
                }
    });
    try {
        Store store  = session.getStore("pop3");
        store.connect("pop.gmail.com" , "username" , "password");
        Folder fldr = store.getFolder("INBOX");
        fldr.open(Folder.HOLDS_MESSAGES);
        int count = fldr.getMessageCount();
        System.out.println(count);
    } catch(Exception exc) {
        System.out.println(exc + " error");
    }

我得到的计数等于 7,但我应该得到 3,因为我的收件箱中只有 3 条消息.

I get the count equal to 7 but i should get 3 because i have only 3 messages in the inbox.

推荐答案

在 GMAIL POP3 设置中,您应该只对当前收到的电子邮件启用 POP 访问,这是标准的 GMAIL 行为.

In GMAIL POP3 settings you should enable POP access only for the emails received from the current moment, it's standard GMAIL behavior.

启用 POP 后,所有邮件都会下载到您的客户端,但垃圾邮件、垃圾邮件和聊天邮件除外.如果您不希望从 Web 界面发送的消息下载到邮件客户端的收件箱,我们建议在您的客户端中创建一个过滤器.您可能需要联系您的邮件客户端的客户服务部门,了解如何对下载的邮件进行分类.

When you enable POP, all messages are downloaded to your client, except for Spam, Trash, and Chats. If you don't want messages that you send from the web interface downloaded to your mail client's inbox, we suggest creating a filter within your client. You may want to contact your mail client's customer service department for instructions on how to categorize downloaded messages.

查看 GMAIL 问题排查文章

GMAIL 中的 AFAIK 选择性同步仅适用于 IMAP 协议.

AFAIK selective sync in GMAIL is only possible with IMAP protocol.

相关文章