Search code examples
javaimapzimbra

how to remove permanently message with javamail and imap


I use imap for reading message from mail server. 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. my mail server is Zimbra.

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

Solution

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

    Calling expunge (JavaDoc) should be as simple as 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.