使用javax.mail删除服务器上的电子邮件 [英] Delete Email on Server using javax.mail

查看:165
本文介绍了使用javax.mail删除服务器上的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用IMAP协议从服务器接收电子邮件,例如描述 here
这工作很好,我可以将电子邮件和附件存储在磁盘上。

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.

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

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.

推荐答案

您应该可以通过标准API来执行此操作。

You should be able to do this via the standard APIs.

首先,您需要获得对要删除的消息(或消息)的引用 - 如果您正在成功阅读它们你已经能够做到这一点了现在,没有明确的delete()操作,但是您可以将消息标记为已删除:

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.setFlag(Flags.Flag.DELETED, true);

这将将邮件标记为已删除(这通常是删除操作在桌面IMAP中执行的操作)客户)。为了强制删除的邮件被删除,当你完成了他们居住的文件夹时,请调用

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

folder.close(true);

其中true标志指示服务器清除所有已删除的邮件。

where the true flag instructs the server to expunge all deleted messages.

瞧瞧!客户端在使用任何IMAP客户端连接到服务器时,不应再看到这些消息。

And voila! The client should no longer see these messages when he connects to the server with any IMAP client.

编辑:

不要忘记以READ_WRITE模式打开文件夹,否则不会从服务器实际删除这些消息。

Don't forget to open the folder in READ_WRITE mode otherwise the messages will not actually be deleted from the server.

folder.open(Folder.READ_WRITE);

请参阅: http://java.sun.com/developer/onlineTraining/JavaMail/contents.html#JavaMailDeleting

这篇关于使用javax.mail删除服务器上的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆