Я выполняю задачу по извлечению gmail. Мне удалось получить с помощью приведенных ниже кодов. Тем не менее, он получил полученную электронную почту от самой ранней до последней электронной почты в папке входящих сообщений Gmail. Есть ли способ заставить его получать последние письма? Я намерен реализовать способ получения только последних 20 писем вместо извлечения всех писем в папке «Входящие». Заранее спасибо за руководство.
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);
}