IllegalWriteException when trying to write flags in JavaMail IMAP

I am currently trying to set a visible flag in an IMAP email as follows:

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

Where messages [] is an array of the Message object filled with downloading all the letters in the folder (for which read / write access is set), and MailNumber is a specific email in the array calculated by the user via email in JTable, which I fill out with your emails letters.

However, this continues to give me this on the second line:

 javax.mail.IllegalWriteException: IMAPMessage is read-only 

Even if I populate the message array (in another function) as follows:

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

What's going on here?

+6
source share
3 answers

IMAP messages are read-only, as are POP3 messages. This is a limitation of the protocol, not JavaMail. The closest thing you can change to change the message is to read the message, create a local copy using the copy constructor of MimeMessage, change the copy, add the copy to the folder and delete the original message.

javadocs for IMAPMessage were accidentally omitted. But then these are not any methods that will help you solve this problem.

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

+3
source

Delete the saveChanges call, this is optional here.

+9
source

Consider using Folder.setFlags() . It even works on IMAPFolder (no IMAPFolder () is needed):

 ... 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); ... 
+3
source

Source: https://habr.com/ru/post/958776/


All Articles