Java 将 IMAPMessage 移动(复制)到邮件服务器上的另一个文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24888072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 15:23:26  来源:igfitidea点击:

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

javaemailjavamailimapjavax.mail

提问by gospodin

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).

我的应用程序正在检查收件箱服务器文件夹上邮件主题的模式,如果发现某种模式,我们应该将电子邮件 (com.sun.mail.imap.IMAPMessage) 移动到另一个文件夹 - 例如名为“test” (副本也可以完成这项工作)。

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?

你能告诉我如何将 IMAPMessage 从收件箱移动/复制到服务器上的另一个文件夹吗?

Thank you

谢谢

采纳答案by Steve

Presumably you're already using a com.sun.mail.imap.IMAPFolder?

想必您已经在使用com.sun.mail.imap.IMAPFolder?

That class has the method addMessages(Message[] msgs). Use it to add a Messageto the new folder.

那个类有方法addMessages(Message[] msgs)。使用它向Message新文件夹添加一个。

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.

或者,正如@gospodin 所提到的,有一种copyMessages(Message[] msgs, Folder destinationFolder)方法可以提供将邮件从原始文件夹复制到新文件夹的快捷方式。

回答by gospodin

        List<Message> tempList = new ArrayList<>();
        tempList.add(myImapMsg);
        Message[] tempMessageArray = tempList.toArray(new Message[tempList.size()]);
        fromFolder.copyMessages(tempMessageArray, destFolder);

回答by Dieter

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 newmessage. The new message have an different Message-IDin 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. IMAPFolderhas the method moveMessages(Message[] msgs, Folder targetFolder)to move messages without tampering the header Message-ID.

这是一个坏主意,移动消息的方法一样copyMessages()addMessages()或者appendMessage()和删除旧邮件,因为这些方法生成一个新的消息。新消息Message-ID的标题有所不同。如果您对新消息做出响应,接收者无法将响应与他发送的邮件相关联,因为他不知道新的Message-ID. 您必须将文件夹强制转换为IMAPFolder. IMAPFolder具有在moveMessages(Message[] msgs, Folder targetFolder)不篡改标题的情况下移动消息的方法Message-ID