简体   繁体   English

Javamail不会获取所有邮件

[英]Javamail does not fetch all messages

I am using the following configuration: 我使用以下配置:

mail.pop3.ssl.enable "true"
mail.pop3s.socketFactory.class "javax.net.ssl.SSLSocketFactory" 
mail.pop3s.socketFactory.fallback "false" 
mail.pop3s.port "995"
mail.pop3s.socketFactory.port "995"
username "...@hotmail.com"
password "..."
host "pop3.live.com"

These properties are defined in an xml file, and loaded by the application into a Properties object. 这些属性在xml文件中定义,并由应用程序加载到Properties对象中。

And the following getter to fetch my emails: 以下getter来获取我的电子邮件:

public Message[] getMessages()
{
    // init variables
    Folder folder = null;
    Store store = null;
    Session session = null;

    // setup session
    try {
        session = Session.getInstance(props,
        new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
        });
        String protocol = host.contains("imap") ? "imaps" : "pop3";
        store = session.getStore(protocol);       
        store.connect(host, username, password);            
    } catch (NoSuchProviderException ex) {
        Logger.getLogger(Fetcher.class.getName()).log(Level.SEVERE, null, ex);
    } catch (MessagingException ex) {
        Logger.getLogger(Fetcher.class.getName()).log(Level.SEVERE, null, ex);
    }

    // read folder          
    try {
        folder = store.getFolder("INBOX");
    } catch (MessagingException ex) {
        Logger.getLogger(Fetcher.class.getName()).log(Level.SEVERE, null, ex);
    }
    try {
         if(!folder.isOpen())
            folder.open(Folder.READ_ONLY);
    } catch (MessagingException ex) {
        Logger.getLogger(Fetcher.class.getName()).log(Level.SEVERE, null, ex);
    }

     // get messages
    try {                       
        return folder.getMessages();
    } catch (MessagingException ex) {
        Logger.getLogger(Fetcher.class.getName()).log(Level.SEVERE, null, ex);
    }

    // default
    return new Message[]{};
}

This code works. 这段代码有效。 It returns my emails. 它会返回我的电子邮件。 But for some reason, it does not return my most recent emails. 但由于某种原因,它不会返回我最近的电子邮件。 It does however always return the emails starting at the same point in time (in other words, it always skips the same emails, so the behaviour is deterministic). 但它始终会返回从同一时间点开始的电子邮件(换句话说,它总是跳过相同的电子邮件,因此行为是确定性的)。 These emails are all in my inbox (not in spam), they are varied, and not special in any way I can conceive. 这些电子邮件都在我的收件箱中(不是垃圾邮件),它们是多种多样的,并且在我能想到的任何方面都不是特别的。

What is going wrong? 出了什么问题?

Update (output of debug up until the first message): 更新(调试输出直到第一条消息):

DEBUG: setDebug: JavaMail version 1.4.5
DEBUG: getProvider() returning  javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc]
DEBUG POP3: mail.pop3s.rsetbeforequit: false
DEBUG POP3: mail.pop3s.disabletop: false
DEBUG POP3: mail.pop3s.forgettopheaders: false
DEBUG POP3: mail.pop3s.cachewriteto: false
DEBUG POP3: mail.pop3s.filecache.enable: false
DEBUG POP3: mail.pop3s.keepmessagecontent: false
DEBUG POP3: mail.pop3s.starttls.enable: false
DEBUG POP3: mail.pop3s.starttls.required: false
DEBUG POP3: mail.pop3s.apop.enable: false
DEBUG POP3: mail.pop3s.disablecapa: false
DEBUG POP3: connecting to host "pop3.live.com", port 995, isSSL true
S: +OK DUB006-POP396 POP3 server ready
C: CAPA
S: -ERR unrecognized command
DEBUG POP3: authentication command trace suppressed
DEBUG POP3: authentication command succeeded
C: STAT
S: +OK 2256 257829688

C: NOOP
S: +OK
C: TOP 1 0
S: +OK

I had the same issue with an MS Exchange Mailbox. 我在MS Exchange邮箱中遇到了同样的问题。 It is just a guess but in my opinion this can be a very common problem when using "modern" Email providers. 这只是猜测,但在我看来,当使用“现代”电子邮件提供商时,这可能是一个非常普遍的问题。

In my case it was because of message threading ("conversation view"): Somehow folder.getMessages() gets the # of conversations in your inbox instead of no. 在我的情况下,这是因为消息线程(“会话视图”):不知何故,folder.getMessages()获取收件箱中的#个会话而不是no。 of messages and ends after that number. 消息并在该号码后结束。 If you have 10 messages in your inbox, but 3 of them are grouped into one conversation, you only get the first 8 messages (7 messages plus 1 conversation). 如果收件箱中有10条消息,但其中3条被分组到一个对话中,则只能获得前8条消息(7条消息加1条对话)。

Solution for me simply was: 我的解决方案就是:

  • turn conversation view off in my inbox folder in the email providers web interface 在电子邮件提供商Web界面的收件箱文件夹中关闭对话视图
  • iterate all mails in an other way, avoiding .getMessages() : 以另一种方式迭代所有邮件, 避免.getMessages()

     for (int i = 1; i < folder.getMessageCount()+1; i++) { Message m = folder.getMessage(i); ... } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM