Felix
Felix

Reputation: 697

Erase deleted Emails in Java Mail

My method to delete emails seems to work, but when I check my mail client I can see that the messages are still there.

@Async
public void deleteEmailAsync(Email email) {
    LOGGER.debug("Deleting email: " + email.getSubject());

    Properties properties = getServerProperties(this.protocol, this.host, this.port);
    Session session = Session.getDefaultInstance(properties);

    try {
        // connects to the message store
        Store store = session.getStore(protocol);
        store.connect(this.userName, this.password);

        // opens the inbox folder
        Folder folderInbox = store.getFolder("INBOX");
        folderInbox.open(Folder.READ_WRITE);

        // fetches new messages from server
        Message[] messages = folderInbox.getMessages();

        for (int i = 0; i < messages.length; i++) {
            Message msg = messages[i];
            Address[] fromAddress = msg.getFrom();
            if (fromAddress[0].toString().contains(email.getFrom()) 
                    && msg.getSubject().equals(email.getSubject()) ) {
                msg.setFlag(Flags.Flag.DELETED, true);
            }
        }

        // disconnect
        folderInbox.close(false);
        store.close();

    } catch (NoSuchProviderException e) {
        LOGGER.error("No provider for protocol: " + protocol + " " + e.getMessage());
    } catch (MessagingException e) {
        LOGGER.error("Could not connect to the message store " + e.getMessage());
    } 
}

enter image description here

Right now I have to manually erase the deleted items, otherwise downloading the INBOX takes with each time longer.

How can I erase the deleted messages ?

Upvotes: 1

Views: 373

Answers (1)

Hermann Stahl
Hermann Stahl

Reputation: 160

You forgot to close the folder! This should make it work.

@Async
public void deleteEmailAsync(Email email) {
    LOGGER.debug("Deleting email: " + email.getSubject());

    Properties properties = getServerProperties(this.protocol, this.host, this.port);
    Session session = Session.getDefaultInstance(properties);

    try {
        // connects to the message store
        Store store = session.getStore(protocol);
        store.connect(this.userName, this.password);

        // opens the inbox folder
        Folder folderInbox = store.getFolder("INBOX");
        folderInbox.open(Folder.READ_WRITE);

        // fetches new messages from server
        Message[] messages = folderInbox.getMessages();

        for (int i = 0; i < messages.length; i++) {
            Message msg = messages[i];
            Address[] fromAddress = msg.getFrom();
            if (fromAddress[0].toString().contains(email.getFrom()) 
                    && msg.getSubject().equals(email.getSubject()) ) {
                msg.setFlag(Flags.Flag.DELETED, true);
            }
        }

        // disconnect
        folderInbox.close(true);
        store.close();

    } catch (NoSuchProviderException e) {
        LOGGER.error("No provider for protocol: " + protocol + " " + e.getMessage());
    } catch (MessagingException e) {
        LOGGER.error("Could not connect to the message store " + e.getMessage());
    } 
}

Upvotes: 1

Related Questions