Phillip Macdonald
Phillip Macdonald

Reputation: 434

IllegalWriteException when trying to write flags in JavaMail IMAP

Currently I am trying to set the seen flag on an IMAP email like this:

messages[EmailNumber].setFlag(Flag.SEEN, true);
messages[EmailNumber].saveChanges();

Where messages[] is an array of Message object populated by loading all the emails in a folder (which is set to have Read/Write access) and EmailNumber is a specific email in the array computed by the user's choice of an Email in a JTable that I am populating with the emails themselves.

However this keeps giving me this on the second line:

javax.mail.IllegalWriteException: IMAPMessage is read-only

Even though I populate the messages array (in a different function) like this:

folder.open(Folder.READ_WRITE);
messages = folder.getMessages();

What's going on here?

Upvotes: 9

Views: 6223

Answers (3)

Alberto Dellaquila
Alberto Dellaquila

Reputation: 61

Consider using Folder.setFlags(). It works even on IMAPFolder (no need for Message.saveChanges()):

...     
IMAPFolder fInbox = store.getFolder("INBOX");
fInbox.open(Folder.READ_WRITE);

SearchTerm searchTerm = <some searchTerm>
Message[] messages = fInbox.search(searchTerm);
fInbox.setFlags(messages, new Flags(Flags.Flag.SEEN), true);
...

Upvotes: 6

Bill Shannon
Bill Shannon

Reputation: 29971

Remove the call to saveChanges, it's unnecessary here.

Upvotes: 10

sumitsabhnani
sumitsabhnani

Reputation: 320

IMAP messages are read-only, just like POP3 messages. It's a limitation of the protocol, not JavaMail. The closest you can get to modifying a message is to read the message, make a local copy using the MimeMessage copy constructor, modify the copy, append the copy to the folder, and delete the original message.

The javadocs for IMAPMessage were accidentally omitted. But then, it doesn't have any methods that will help you solve this problem.

Refer : https://forums.oracle.com/thread/1589468

Upvotes: 9

Related Questions