2012-01-18 9 views
3

gmailsを取得するタスクを実行中です。私は下のコードで検索することができます。ただし、Gmailの受信トレイにある最新の電子メールから受信した電子メールを取得しました。最新のメールを取得する方法はありますか?私は、受信箱内のすべてのメールを検索するのではなく、最新の20件のメールを検索する方法を実装しようと考えています。ガイダンスのために事前に感謝します。 android.os.MessageAndroidでjavamailを使用して日付の時刻に合わせてメールを取得する

static class DateCompare implements Comparator<Message> { 

      public int compare(Message one, Message two){ 

      return one.getWhen().compareTo(two.getWhen());   
      }   
     } 

............ 

DateCompare compare = new DateCompare(); 
    Message messages[]; 
     if(recent){ 
      messages = inbox.search(new FlagTerm(new Flags(Flag.RECENT), false)); 
     }else{ 
      messages = inbox.search(new FlagTerm(new Flags(Flag.SEEN), false)); 
     }  
      List<Message> list = Arrays.asList(messages); 
      Collections.sort(list,compare); 
      List<Messsage> newList = list.subList(0,19); 

を使用し

public ArrayList<HashMap<String, String>> getMail(int inboxList){ 
     Folder inbox; 
     /*&nbsp; Set the mail properties&nbsp; */ 
     Properties props = System.getProperties(); 
     props.setProperty("mail.store.protocol", "imaps"); 
     try 
     { 
      /*&nbsp; Create the session and get the store for read the mail. */ 
      Session session = Session.getDefaultInstance(props, null); 
      Store store = session.getStore("imaps"); 
      store.connect("imap.gmail.com",username, password); 

      /*&nbsp; Mention the folder name which you want to read. */ 
      inbox = store.getFolder("Inbox"); 
      System.out.println("No of Unread Messages : " + inbox.getUnreadMessageCount()); 
      /*Open the inbox using store.*/ 
      inbox.open(Folder.READ_WRITE); 
      /*&nbsp; Get the messages which is unread in the Inbox*/ 
      Message messages[]; 
      if(recent){ 
       messages = inbox.search(new FlagTerm(new Flags(Flag.RECENT), false)); 
      }else{ 
       messages = inbox.search(new FlagTerm(new Flags(Flag.SEEN), false)); 
      } 

      /* Use a suitable FetchProfile&nbsp;&nbsp;&nbsp; */ 
      FetchProfile fp = new FetchProfile(); 
      fp.add(FetchProfile.Item.ENVELOPE); 
      fp.add(FetchProfile.Item.CONTENT_INFO); 
      inbox.fetch(messages, fp); 

      try 
      { 
       printAllMessages(messages); 
       inbox.close(true); 
       store.close(); 
      } 
      catch (Exception ex) 
      { 
       System.out.println("Exception arise at the time of read mail"); 
       ex.printStackTrace(); 
      } 
     } 
     catch (NoSuchProviderException e) 
     { 
      //e.printStackTrace(); 

      System.exit(1); 

     } 
     catch (MessagingException e) 
     { 
      //e.printStackTrace(); 

      System.exit(2); 
     } 
+0

を、私はメッセージのgetWhen()その後、Collections.sort(messageList、yourcomparator)を使用して、コンパレータを実装します。このgetWhen()によって順序付けされたリストの最初から20個を取り出します。 –

答えて

10

代わりのinbox.search()使用

inbox.getMessages(int start,int end); 

を使用して、メッセージの範囲を取得します。最新の20件のメールを取得するには

は使用:

int n=inbox.getMessageCount(); 
messages= inbox.getMessages(n-20,n); 
2

このことができます願っています。

3

最後の20個のメッセージが必要な場合は、最後の20個のメッセージ番号 を要求するか、folder.getMessages()によって返された配列の最後の20個のエントリにアクセスしてください。 メールボックス内のメッセージの順序は、到着した順序ではありません。 は、送信された順序ではありません。

関連する問題