Hi, the JavaDocs for the Folder indicate once the folder is closed you can't use it again with the same message.
Message objects from a Folder are only valid while a Folder is open and should not be accessed after the Folder is closed, even if the Folder is subsequently reopened. Instead, new Message objects must be fetched from the Folder after the Folder is reopened.
I just got this to work (with IMAP) ...
Code:
Folder folder = message.getFolder();
folder.open(Folder.READ_WRITE);
String messageId = message.getMessageID();
Message[] messages = folder.getMessages();
FetchProfile contentsProfile = new FetchProfile();
contentsProfile.add(FetchProfile.Item.ENVELOPE);
contentsProfile.add(FetchProfile.Item.CONTENT_INFO);
contentsProfile.add(FetchProfile.Item.FLAGS);
folder.fetch(messages, contentsProfile);
for (int i = 0; i < messages.length; i++) {
if (((MimeMessage) messages[i]).getMessageID().equals(messageId)) {
messages[i].setFlag(Flags.Flag.DELETED, true);
break;
}
}
folder.expunge();
folder.close(true);
That said, I am working on a fix (INT-2803) because, right now, the folder in the message is the same instance as that in the MailReceiver so you could get collisions. So, until RC3 comes out, it would be safer to use
Code:
...
URLName url = new URLName("INBOX");
Folder folder = message.getFolder().getStore().getFolder(url);
...
instead of message.getFolder();