簡體   English   中英

Javamail不會獲取所有郵件

[英]Javamail does not fetch all messages

我使用以下配置:

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"

這些屬性在xml文件中定義,並由應用程序加載到Properties對象中。

以下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[]{};
}

這段代碼有效。 它會返回我的電子郵件。 但由於某種原因,它不會返回我最近的電子郵件。 但它始終會返回從同一時間點開始的電子郵件(換句話說,它總是跳過相同的電子郵件,因此行為是確定性的)。 這些電子郵件都在我的收件箱中(不是垃圾郵件),它們是多種多樣的,並且在我能想到的任何方面都不是特別的。

出了什么問題?

更新(調試輸出直到第一條消息):

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

我在MS Exchange郵箱中遇到了同樣的問題。 這只是猜測,但在我看來,當使用“現代”電子郵件提供商時,這可能是一個非常普遍的問題。

在我的情況下,這是因為消息線程(“會話視圖”):不知何故,folder.getMessages()獲取收件箱中的#個會話而不是no。 消息並在該號碼后結束。 如果收件箱中有10條消息,但其中3條被分組到一個對話中,則只能獲得前8條消息(7條消息加1條對話)。

我的解決方案就是:

  • 在電子郵件提供商Web界面的收件箱文件夾中關閉對話視圖
  • 以另一種方式迭代所有郵件, 避免.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