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;
/* Set the mail properties */
Properties props = System.getProperties();
props.setProperty("mail.store.protocol", "imaps");
try
{
/* 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);
/* 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);
/* 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 */
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);
}
を、私はメッセージのgetWhen()その後、Collections.sort(messageList、yourcomparator)を使用して、コンパレータを実装します。このgetWhen()によって順序付けされたリストの最初から20個を取り出します。 –