Ordering list of messages is supported in IMAP but it cannot be
implemented with current JavaMail 1.4.3 API. The Current API
implementation requires to implement IMAPFolder.ProtocolCommand to
sort incoming mail which is not suitable solution due to
implementation issues.
The com.sun.mail.imap.IMAPFolder doCommand currently supports sorting
of messages according to sort type. The IMAPFolder API support shows a
good example how this feature can be achieved.
see
http://java.sun.com/products/javamail/javadocs/com/sun/mail/imap/IMAPFolder.html#doCommand(com.sun.mail.imap.IMAPFolder.ProtocolCommand)
I tried this approach few times and always got "A4 bad unknow
command : SORT" and did not help at all to to sort messages.Then using
a comparator was the next worst thing which is again not the best to
work with.
After a long session of research I found out that according to RFC
5256[1] the JavaMail 1.4.4[2] has methods directly to support IMAP
sorting which is still under implementation.The good news is the new
JavaMail API contains methods such as getSortedMessages(SortTerm[]
term, SearchTerm sterm) which directly returns a sorted Message[] or
can use SearchTerm object. The bad news is JavaMail 1.4.4 RC is still
not released and once released this feature will be implemented.
[1]
http://tools.ietf.org/search/rfc5256]
[2]
http://kenai.com/projects/javamail/