2017-02-24 11 views
0

gmailの受信トレイ[プライマリ]からレポートを生成するJavaコードを作成しましたが、Javaコードの実行時に作成しました。すべての受信メール[Primary、Social]と送信メールも生成しています。クラスパスにすでにJavax.mail.jarとactivation.jarが含まれています。 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

私はあなたに別のアプローチを提案します。 Google REST APIをご覧ください。メールアカウントのクエリに使用できる呼び出しがあります。

そのはかなりまっすぐ進む実際

あなたはラベルを使用してメッセージを照会することができます。ラベルには、システムラベルとユーザーラベルの2種類があります。

Try It! Googleと機能が出て

ゲットラベル、それをテストする:指定したラベルの https://developers.google.com/gmail/api/v1/reference/users/labels/list

、あなたはまた、スターを付けたために主演し、CATEGORY_PERSONALを得ることができますあなたのケースでCATEGORY_PRIMARYがメッセージ https://developers.google.com/gmail/api/v1/reference/users/messages/list

を照会言います受信トレイからの重要な電子メール。

+0

正確なレポートを生成するための完全なコードを記述してください。 – user7616778

+0

これはあなたのために働いていません。問題があれば、私たちはあなたを助けます。試してみて、コードを投稿してヘルプを求めてください – rakwaht

関連する問題