2017-08-29 56 views
0

嗨我想獲取Gmail收件箱的所有郵件,但它沒有返回所有郵件。它返回的郵件太舊了。不是最新的。我用下面的代碼測試:Java郵件Api不返回所有郵件

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. 我也想只獲得主要選項卡郵件。如何在我的代碼中應用選項卡級別篩選器?

+0

主要選項卡?如果你正在談論Gmail,請用標籤指定它。 – Clijsters

+0

@Clijsters:是的,我正在嘗試使用Gmail。 –

回答

0

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

0

here

Q摘自:爲什麼我看不到我的所有郵件與POP3訪問Gmail時?

答:Gmail的設置可通過POP3協議控制您的哪些郵件可用 。查看Gmail設置頁面以更改您的Gmail帳戶的 配置。

關於POP3:

POP3是用於訪問單個郵箱非常有限的協議。它比IMAP的能力低得多

相關問題