3

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
srr7
  • 151
  • 1
  • 11

2 Answers2

6

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.

Max
  • 10,701
  • 2
  • 24
  • 48
0
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

srr7
  • 151
  • 1
  • 11
  • 2
    The best way to give thanks to someone is to vote up and accept their answer. – Max Sep 26 '16 at 13:25