2017-02-24 107 views
0

我已經寫了一個java代碼來生成報告從Gmail收件箱[小學],但在執行java代碼的時候。它會生成所有收到的郵件[Primary,Social]併發送郵件。我已經將Javax.mail.jar和activation.jar包含到classpath中。 請幫我只生成Gmail收件箱[主]的報告。 我的代碼如下: -電子郵件收件箱報告不正確生成

import java.util.Properties; 
    import javax.mail.Authenticator; 
    import javax.mail.Folder; 
    import javax.mail.Message; 
    import javax.mail.MessagingException; 
    import javax.mail.NoSuchProviderException; 
    import javax.mail.PasswordAuthentication; 
    import javax.mail.Session; 
    import javax.mail.Store; 

    public class CheckingMails2 { 

     public static void check(String host, String storeType, String user, 
      String password) 
     { 
      try { 

      // create properties field 
      Properties properties = new Properties(); 

      properties.put("mail.pop3s.host", host); 
      properties.put("mail.pop3s.port", "995"); 
      properties.put("mail.pop3s.starttls.enable", "true"); 

      // Setup authentication, get session 
      Session emailSession = Session.getInstance(properties, 
      new javax.mail.Authenticator() { 
       protected PasswordAuthentication getPasswordAuthentication() { 
        return new PasswordAuthentication(
         "[email protected]", "********"); 
       } 
      }); 
      // emailSession.setDebug(true); 

      // create the POP3 store object and connect with the pop server 
      Store store = emailSession.getStore("pop3s"); 

      store.connect(); 

      // 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); 

      for (int i = 0, n = messages.length; i < n; i++) { 
      Message message = messages[i]; 
      System.out.println("---------------------------------"); 
      System.out.println("Email Number " + (i + 1)); 
      System.out.println("Subject: " + message.getSubject()); 
      System.out.println("From: " + message.getFrom()[0]); 
      System.out.println("Text: " + message.getContent().toString()); 
      System.out.println("SentDate: " + message.getSentDate().toString()); 
      } 

      //String sentDate = message.getSentDate().toString(); 

      // 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";// change accordingly 
      String mailStoreType = "pop3"; 
      String username = "[email protected]";// change accordingly 
      String password = "********";// change accordingly 

      check(host, mailStoreType, username, password); 

     } 

    } 

回答

0

我建議你採用不同的方法。只需看看谷歌REST API,你會發現可以用來查詢你的電子郵件帳戶的電話。

其實很簡單

您可以使用標籤查詢消息。標籤有兩種類型系統標籤和用戶標籤。

您可以使用Try It!與谷歌的功能來測試它

獲取標籤: https://developers.google.com/gmail/api/v1/reference/users/labels/list

對於給定的標籤,說CATEGORY_PRIMARY在您的案件查詢消息 https://developers.google.com/gmail/api/v1/reference/users/messages/list

您還可以得到出演,CATEGORY_PERSONAL已加星標的和來自收件箱的重要電子郵件。

+0

請幫我寫完整的代碼來生成準確的報告。 – user7616778

+0

這樣我們就不適合你了。如果您遇到問題,我們會幫助您。嘗試一下,然後發佈你的代碼尋求幫助 – rakwaht