使用javax.mail删除服务器上的电子邮件

时间:2022-09-04 18:13:46

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.

问题:当客户端尝试接收所有电子邮件时,我是否有可能从服务器中删除文件以使它们不再可用?如果是这样,请告诉我如何。

1 个解决方案

#1


61  

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:

首先,您需要获取对要删除的消息(或多个消息)的引用 - 如果您已成功阅读它们,那么您已经能够执行此操作。现在,没有明确的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.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

#1


61  

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:

首先,您需要获取对要删除的消息(或多个消息)的引用 - 如果您已成功阅读它们,那么您已经能够执行此操作。现在,没有明确的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.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