使用 javax.mail 删除服务器上的电子邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1464706/
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
Delete Email on Server using javax.mail
提问by Markus Lausberg
I am receiving emails from the server using the IMAP protocol like it is described here. This is working very fine and I can store the emails and attachments on the disk.
我正在使用 IMAP 协议从服务器接收电子邮件,就像这里描述的那样。这工作得很好,我可以将电子邮件和附件存储在磁盘上。
Question: Do I have the possibility to delete files from the Server, so that they are no longer available, when a client tries to receive all emails? If so, please tell me how.
问题:当客户端尝试接收所有电子邮件时,我是否可以从服务器中删除文件,使它们不再可用?如果是这样,请告诉我如何。
采纳答案by Andrzej Doyle
You should be able to do this via the standard APIs.
您应该能够通过标准 API 执行此操作。
First you need to get a reference to the Message
(or messages) you want to delete - if you're successfully reading them then you're already able to do this. Now, there's no explicit delete() operation, but you can mark a message as deleted like so:
首先,您需要获得对Message
要删除的(或消息)的引用- 如果您成功阅读了它们,那么您已经能够执行此操作。现在,没有显式的 delete() 操作,但是您可以像这样将消息标记为已删除:
message.setFlag(Flags.Flag.DELETED, true);
This will mark the message as deleted (which is typically what a delete operation will do in a desktop IMAP client). In order to force the deleted messages to be expunged, when you're finished with the Folder
(s) in which they reside, call
这会将邮件标记为已删除(这通常是桌面 IMAP 客户端中的删除操作将执行的操作)。为了强制删除已删除的消息,当您完成Folder
它们所在的(s)时,请致电
folder.close(true);
where the true flag instructs the server to expunge all deleted messages.
其中 true 标志指示服务器清除所有已删除的邮件。
And voila! The client should no longer see these messages when he connects to the server with any IMAP client.
瞧!当客户端使用任何 IMAP 客户端连接到服务器时,他不应再看到这些消息。
EDIT:
编辑:
Don't forget to open the folder in READ_WRITE mode otherwise the messages will not actually be deleted from the server.
不要忘记以 READ_WRITE 模式打开文件夹,否则邮件实际上不会从服务器中删除。
folder.open(Folder.READ_WRITE);
See: http://java.sun.com/developer/onlineTraining/JavaMail/contents.html#JavaMailDeleting
请参阅:http: //java.sun.com/developer/onlineTraining/JavaMail/contents.html#JavaMailDeleting