Search code examples
javaimapjakarta-mail

How does JavaMail API expunge messages when IMAP folder is closed


I wonder, how JavaMail API expunges IMAP messages when Folder.close(true) is being called? According to the docs, the only parameter "expunge" of method "close" determines whether to expunge all messages that have been previously marked with a \Deleted flag. In the source code (V. 1.4.7), however, I cannot see any piece of code in the method close (class IMAPFolder), that expunges anything or does anything even going into that direction. Can anyone help me understand the effect of the expunge parameter?

Thank you very much in advance and have a nice day.


Solution

  • IMAPFolder.class, line 1310 :

     // If the expunge flag is set, close the folder first.
     if (expunge && protocol != null)
        protocol.close();
    

    IMAPProtocol.class, line 1201 :

     /**
     * CLOSE Command.
     *
     * @see "RFC2060, section 6.4.2"
     */
    public void close() throws ProtocolException {
       simpleCommand("CLOSE", null);
    }
    

    https://www.rfc-editor.org/rfc/rfc2060#section-6.4.2 :

    The CLOSE command permanently removes from the currently selected mailbox all messages that have the \Deleted flag set, and returns to authenticated state from selected state. No untagged EXPUNGE responses are sent.

    No messages are removed, and no error is given, if the mailbox is selected by an EXAMINE command or is otherwise selected read-only.

    Even if a mailbox is selected, a SELECT, EXAMINE, or LOGOUT command MAY be issued without previously issuing a CLOSE command. The SELECT, EXAMINE, and LOGOUT commands implicitly close the currently selected mailbox without doing an expunge. However, when many messages are deleted, a CLOSE-LOGOUT or CLOSE-SELECT