IllegalStateException: Folder is not Open

Jonathan Melhuish <[email protected]>
Newsgroups gmane.comp.java.classpath.extensions.javamail
Message-ID <[email protected]>
Hi,

I'm trying to write a simple program that downloads messages from a POP3
server and writes them to disk.  I've successfully used the Sun
libraries to download the messages, but I need to use the GNU Javamail
implementation to save the messages to an Mbox file.  I couldn't find
much in the way of usage instructions or code examples (which is a
shame), but through a bit of guesswork I came up with this:

public void storeMessage(MimeMessage message) {
            URLName mbox = new URLName("mbox:///home/jon/epmail/test");
            Store store = session.getStore(mbox);
            Folder root = store.getDefaultFolder();
            if(!root.exists()) root.create(Folder.HOLDS_MESSAGES);
            root.open(Folder.READ_WRITE);

            // Add message to store
            MimeMessage[] messagearray = new MimeMessage[1];
            messagearray[0] = message;
            root.appendMessages(messagearray);
            root.releaseLock();
}

When running this code, I get:

Exception in thread "main" java.lang.IllegalStateException: Folder is
not Open
    at com.sun.mail.pop3.POP3Folder.checkOpen(POP3Folder.java:414)
    at com.sun.mail.pop3.POP3Folder.getProtocol(POP3Folder.java:441)
    at com.sun.mail.pop3.POP3Message.getSize(POP3Message.java:92)
    at javax.mail.internet.MimeMessage.<init>(MimeMessage.java:189)
    at gnu.mail.providers.ReadOnlyMessage.<init>(ReadOnlyMessage.java:73)
    at gnu.mail.providers.mbox.MboxMessage.<init>(MboxMessage.java:83)
    at 
gnu.mail.providers.mbox.MboxFolder.appendMessages(MboxFolder.java:558)
    at com.qinetiq.dtg.email.MailClient.storeMessage(MailClient.java:150)
    at com.qinetiq.dtg.email.MainTimer.main(MainTimer.java:59)

Even though Folder.exists() returns true, Folder.isOpen() also returns
true and an empty file is created in the desired location.

The mentions of POP3Folder in the above error concerns me somewhat, but
I'm not sure where that's coming from as the argument should now be a
MimeMessage.  I tried casting the MimeMessage to an MboxMessage just for
good measure, but got a ClassCastException.

If I comment out the Folder.create() line and delete the output file, 
Folder.exists() still returns true, but Folder.open(Folder.READ_WRITE) 
gives a "MessagingException: Folder is read-only".  I'm not sure if this 
is relevant, but I can't find any meaningful documentation for 
Folder.create.

Anybody got any ideas?

Cheers,

Jon
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.