Example of deleting email using JavaMail API

Source: https://github.com/i88ca/example-of-deleting-email-using-java-mail-api

/**
 * Example of deleting email using JavaMail API. It is
 * not following the best practice. It is just for example of some of the API functions.
 * In practice you should use command of IMAPProtocol instead.
 *
 */
public class App {

    public static void main(String[] args) throws NoSuchProviderException, MessagingException {
        Properties prop = new Properties();
        try {
            //load a properties file from class path, inside static method
            prop.load(new FileInputStream("newproperties.properties"));
        } catch (IOException ex) {
            Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
        }

        Properties props = System.getProperties();
        props.setProperty("mail.pop3.port", prop.getProperty("port"));
        Session session = Session.getInstance(props);

        Store store = session.getStore("imap");

        store.connect(prop.getProperty("host"), prop.getProperty("user"), prop.getProperty("passwd"));

        javax.mail.Folder[] dfolders = store.getDefaultFolder().list("*");
        for (Folder f : dfolders) {
            if (!"INBOX".equalsIgnoreCase(f.getFullName())) {
                continue; //only process inbox folder
            }

            if ((f.getType() & javax.mail.Folder.HOLDS_MESSAGES) == 0) {
                continue; // no problem for inbox folder, but need this for some other folder
            }
            IMAPFolder folder = (IMAPFolder) f;

            int c = folder.getMessageCount();
            for (int i = 1; i < c; i++) {
                if (!folder.isOpen()) {
                    folder.open(Folder.READ_WRITE);
                }
                Message message = folder.getMessage(i);
                message.setFlag(Flags.Flag.DELETED, true); //marked this message as deleted
                //folder.expunge(new Message[]{message});
                // for some email server, it throws javax.mail.FolderClosedException: * BYE [ALERT] Fatal error: Cannot allocate memory
                if (folder.isOpen() && i % 10 == 0) {
                    System.out.println(folder.getMessageCount());
                    folder.close(true); //open and close can avoid the FolderClosedException from folder.expunge.
                }            }
            if (folder.isOpen()) {
                folder.close(true);
            }
        }
    }
}

Popular posts from this blog

How to delete / clear queue of PowerMTA