Reputation: 1201
My application is checking the patterns of the subjects of the mails on the Inbox server folder and if some pattern is found, we should move the email (com.sun.mail.imap.IMAPMessage) to another folder - called 'test' for example (copy will do the job also).
I searched on the Internet for the solution but I could not find anything helpful.
Can you tell me how can I move / copy IMAPMessage from inbox to another folder on server?
Thank you
Upvotes: 13
Views: 23117
Reputation: 111
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
.
Upvotes: 11
Reputation: 1201
List<Message> tempList = new ArrayList<>();
tempList.add(myImapMsg);
Message[] tempMessageArray = tempList.toArray(new Message[tempList.size()]);
fromFolder.copyMessages(tempMessageArray, destFolder);
Upvotes: 5
Reputation: 9480
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.
Upvotes: 7