简体   繁体   中英

how javamail imap fetch mail order by receive date desc

how javamail imap fetch mail order by receive date desc? folder.getMessage() no a date arg. I want to sort by date when fetch mail in imap. Thanks Advance!

Normally, messages are stored in the INBOX in the order they're received, so message number order is received date order. But note that this can be wrong if messages are moved between folders.

In general, if you want messages in a particular order, you'll need to sort them. If your IMAP server supports the SORT extension, you can ask the server to do the sorting by using the com.sun.mail.imap.IMAPFolder.getSortedMessages method .

    @DefaultValue("REVERSE,ARRIVAL") MailSortTerms sortTerms
    
/**/


                        if (imapStore.hasCapability("SORT*")) {
                            Message[] messages = ((IMAPFolder) inbox).getSortedMessages(
                                    sortTerms.getTerms());
                            for (int i = skip;
                                    i < Math.min(skip + size, inbox.getMessageCount());
                                    i++) {
                                resultList.add(messages[i]);
                            }
                        } else {
                            Message[] messages = inbox.getMessages();
                            for (int i = inbox.getMessageCount() - skip - 1;
                                    i >= Math.max(inbox.getMessageCount() - skip - size - 1, 0);
                                    i--) {
                                resultList.add(messages[i]);
                            }
                        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM