我想使用imapFolder.getSortedMessages(SortTerm[] term);
方法来获取邮件并按发送日期排序。
public List<MailMessage> load(SortTerm[] term,)
throws MessagingException, UnsupportedEncodingException {
List<MailMessage> mailMessages = new ArrayList<MailMessage>();
term[0] = SortTerm.DATE;
Store store = getStore();
Folder folder = store.getFolder("INBOX");
if (folder != null) {
IMAPFolder imapFolder = (IMAPFolder) folder;
imapFolder.open(Folder.READ_WRITE);
Message[] messages = imapFolder.getSortedMessages(term);
FetchProfile fp = new FetchProfile();
fp.add(UIDFolder.FetchProfileItem.UID);
fp.add(FetchProfile.Item.ENVELOPE);
folder.fetch(messages, fp);
UIDFolder uidFolder = (UIDFolder) folder;
for (Message message : messages) {
Long uid = uidFolder.getUID(message);
MimeMessage msg = (MimeMessage) message;
MailMessage mailMessage = new MailMessage(msg, uid);
mailMessages.add(mailMessage);
}
}
return mailMessages;
}
但它对我不起作用。它不按发送日期排序。对我的程序有任何建议。