Question

I'm trying to have an email-processing Java application move all processed mails from an IMAP inbox to a subfolder. If that subfolder does not exist, it should create it. This last bit is what doesn't work.

The code snippet is:

private void _backupMessage(Message msg, Folder folder, String sBackupFolderName) throws MessagingException
{
    Folder backupFolder = folder.getFolder(sBackupFolderName);
    if (!backupFolder.exists()) {
        boolean f = backupFolder.create(Folder.HOLDS_FOLDERS & Folder.HOLDS_MESSAGES);
        if (!f) {
            this._triggerFaultEvent(new RuntimeException("Could not create backup folder."));
        }
    }
    backupFolder.open(Folder.READ_WRITE);
    folder.copyMessages(new Message[] { msg }, backupFolder);
    backupFolder.close(true);
}

The corrsponding Javadoc is here, but it really doesn't say anything except that if create() returns false, the folder wasn't created (surprise, surprise).

I was able to create the folder using Thunderbird with the same account.

My email server (Postfix) didn't show any log entries, except where it couldn't find the new folder. In the corresponding unit test, the mock email server (GreenMail) either works or ignores the command, in any case, the test passes.

Was it helpful?

Solution

First of all, Postfix is not an IMAP server. There should be another program serving IMAP for you and that is the place to check the logs, not Postfix.

When exactly false is returned depends on implementation. SUN's Javamail implementation will return false if

  1. IMAP command to create a folder has failed, OR
  2. IMAP command was successful, but the folder still did not exist, as checked with exists(), OR
  3. If you ask for a folder which can hold subfolders, it will also check that the created folder doesn't have \Noinferiors flag. This is because some IMAP servers don't support folders with both messages and subfolders.

I suggest you get a source for your implementation and step through it with a debugger. This is probably the quickest way to find out what exactly is wrong.

OTHER TIPS

Beware:

Folder.HOLDS_MESSAGES == 1

Folder.HOLDS_FOLDERS == 2

Folder.HOLDS_FOLDERS & Folder.HOLDS_MESSAGES == 0 (always)

You may try: Folder.HOLDS_FOLDERS | Folder.HOLDS_MESSAGES which equals 3

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top