How can I rename a folder without creating a new one and moving the messages?

Andrea Pompili

If you're using 1.1.3 version of JavaMail you can use this method of the Folder class:

boolean renameTo(Folder f) throws MessagingException
You have to create a new Folder object with the desired new name and then apply this method to the Folder you want to rename.
If something goes wrong (wrong name, not existing folder, already existing folder...) it returns FALSE.

You must take care of two things:
  • The folder you want to rename must be closed.
  • This works only for IMAP protocol (or others similar), if you call this method in a POP3 environment you obtain a MethodNotSupportedException.