move (copy) IMAPMessage to another folder on the mail server
List<Message> tempList = new ArrayList<>();
tempList.add(myImapMsg);
Message[] tempMessageArray = tempList.toArray(new Message[tempList.size()]);
fromFolder.copyMessages(tempMessageArray, destFolder);
It is a bad idea to move a message with methods like copyMessages()
, addMessages()
or appendMessage()
and removing the old message, because these methods generates a new message. The new message have an different Message-ID
in the header. If you response on the new message, the receiver cannot relate the response to his sent mail, because he does not know the new Message-ID
. You have to cast the folder to a IMAPFolder
. IMAPFolder
has the method moveMessages(Message[] msgs, Folder targetFolder)
to move messages without tampering the header Message-ID
.
Presumably you're already using a com.sun.mail.imap.IMAPFolder
?
That class has the method addMessages(Message[] msgs)
. Use it to add a Message
to the new folder.
Alternatively, as mentioned by @gospodin, there's a copyMessages(Message[] msgs, Folder destinationFolder)
method, which provides a shortcut for copying messages from their original folder to a new one.