move (copy) IMAPMessage to another folder on the mail server
Asked Answered
G

3

13

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

Gush answered 22/7, 2014 at 12:57 Comment(2)
im wondering if this may work: List<Message> tempList = new ArrayList<>(); tempList.add(myImapMsg); Message[] tempMessageArray = tempList.toArray(new Message[tempList.size()]); destFolder.copyMessages(tempMessageArray, fromFolder);Gush
Yes, use the copyMessages method. I'm always curious as to where exactly people search on the internet for things like this but their search doesn't involve actually reading the documentation for the APIs they're using. Can you explain why it is that you didn't think reading the javadocs would be helpful, or how it is that you read them but didn't find the copyMessages method? Perhaps there's something I can do to improve the JavaMail documentation to be more helpful for simple cases like this?Extrados
C
7

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.

Chloromycetin answered 22/7, 2014 at 14:40 Comment(2)
Using yahoo mail I get A4 BAD [CLIENTBUG] COPY Bad sequence in the command when I try use copyMessages(Message[] msgs, Folder destinationFolder)Countenance
Going by mailman13.u.washington.edu/pipermail/imap-protocol/2013-March/… ... this may just be a Yahoo issue.Chloromycetin
B
11

New messages are generated by copy, append, add

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.

Cast to IMAPFolder

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.

Browder answered 2/12, 2019 at 11:11 Comment(2)
FWIW copyMessage() does not generate a new message-id, and is the best fallback if moveMessages() fails (which It can do).Whitleather
I had a problem with outlook365 which didn't let my copy(Inbox => Deleted Items)/then delete (delete failed without error) but moveMessages to "Deleted Items" worked. So considering copyMessage as a fallback if moveMessages fails? Better doubled check.Lexington
C
7

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.

Chloromycetin answered 22/7, 2014 at 14:40 Comment(2)
Using yahoo mail I get A4 BAD [CLIENTBUG] COPY Bad sequence in the command when I try use copyMessages(Message[] msgs, Folder destinationFolder)Countenance
Going by mailman13.u.washington.edu/pipermail/imap-protocol/2013-March/… ... this may just be a Yahoo issue.Chloromycetin
G
5
        List<Message> tempList = new ArrayList<>();
        tempList.add(myImapMsg);
        Message[] tempMessageArray = tempList.toArray(new Message[tempList.size()]);
        fromFolder.copyMessages(tempMessageArray, destFolder);
Gush answered 22/7, 2014 at 14:21 Comment(3)
Could you add some explanation of this code? Answers that are purely a code block tend to be less helpful.Tertia
According to the Java documentation, it's fromFolder.copyMessages(tempMessageArray, toFolder);Chloromycetin
I'm summarizing in a single row: destFolder.copyMessages(new Message[]{myImapMsg}, fromFolder);Workman

© 2022 - 2024 — McMap. All rights reserved.