简体   繁体   English

如何使用javamail和imap删除永久消息

[英]how to remove permanently message with javamail and imap

I use imap for reading message from mail server. 我使用imap从邮件服务器读取消息。 I want when i read message, the message delete from mail server. 我希望当我阅读邮件时,邮件从邮件服务器中删除。 I use javaMail library and set delete flag to true and i can not see message from web panel but when i get count of message, the count of message dose not changed. 我使用javaMail库并将delete标志设置为true,我无法从网络面板看到消息,但是当我得到消息计数时,消息计数没有改变。 my mail server is Zimbra. 我的邮件服务器是Zimbra。

int count = inbox.getMessageCount();//for example count=100
inbox[i].setFlag(Flags.Flag.DELETED, true);
count = inbox.getMessageCount();// count=100

You need to expunge the messages after marking them deleted for them actually to be removed from the folder. 标记删除后,您需要删除邮件,以便实际从文件夹中删除它们。 In the meantime, they just sit around with a \\Deleted flag, and most IMAP clients will hide them. 与此同时,他们只是坐着一个\\Deleted标志,大多数IMAP客户端都会隐藏它们。

Calling expunge ( JavaDoc ) should be as simple as inbox.expunge() . 调用expunge( JavaDoc )应该像inbox.expunge()一样简单。 This will cause any messages you've marked deleted, or possibly marked deleted in another session, to be removed, and will renumber the existing message sequence numbers in all other messages. 这将导致您标记为已删除或可能在另一个会话中标记为已删除的邮件被删除,并将重新编号所有其他邮件中的现有邮件序列号。

If your server supports UIDPLUS and you need more control, IMAPFolder.expunge() supports expunging a specific list of DELETED messages. 如果您的服务器支持UIDPLUS并且您需要更多控制,则IMAPFolder.expunge()支持删除特定的DELETED消息列表。

if (inbox.isOpen()) {    
    Message[] messages = inbox.getMessages();
for (int i = 0; i < messages.length; i++) {
    System.out.println( messages[i]);
     messages[i].setFlag(Flags.Flag.DELETED, true);
 }
if (inbox.isOpen()) {
    inbox.expunge();
  }
}

Thanks @Max 谢谢@Max

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM