Search code examples
javaandroidemailimap

java mail - getting message by message ID


I'm developing Android mail client. I need to build a "conversation" structure for every email message. I use the

IMAPMessage.getInReplyTo()

method that returns the Message ID of message which the message is a reply to. Unfortunatelly there seems to be no easy way to obtain message from

IMAPFolder

using its message ID. It is only possible to get the message by its UID. Is there an easy way to get the IMAP message by its Message ID?


Solution

  • You can use the IMAPFolder's search method like this:

    SearchTerm searchTerm = new MessageIDTerm(messageId);
    Message[] messages = imapFolder.search(searchTerm);
    

    See the docs for the IMAPFolder's search method here: https://javaee.github.io/javamail/docs/api/com/sun/mail/imap/IMAPFolder.html#search-javax.mail.search.SearchTerm-

    and for the MessageIDTerm class here: https://javaee.github.io/javamail/docs/api/javax/mail/search/MessageIDTerm.html