Java Email Message delete email

Description

Java Email Message delete email

import java.util.Properties;

import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Store;

public class Main {
   public static void main(String[] args) throws Exception {
      String host = "pop.gmail.com", protocol = "imaps";
      String username = "usr.some", password = "some.usr";

      Properties props = new Properties();
      Session session = Session.getInstance(props);
      // session.setDebug(true);
      Store store = session.getStore(protocol);
      store.connect(host, username, password);
      Folder inbox = store.getFolder("Inbox");
      if (inbox.exists()) {
         inbox.open(Folder.READ_WRITE);/*from w w  w  .j  a  va  2 s  .  c o m*/
         Message[] emails = inbox.getMessages();
         for (int i = 0; i < emails.length; i++) {
            System.out.println("Deleting Message " + (i + 1));
            emails[i].setFlag(Flags.Flag.DELETED, true);
         }
         inbox.close(true);
      } else
         System.out.println("Inbox not available");
      store.close();
   }
}



PreviousNext

Related