Javamail 消息,在文件夹以只读模式打开时设置为可见?

Javamail Message, set as seen while folder is open in read only mode?

我有一个 IMAP Folder 打开为 READ_ONLY,我想在程序的特定点将特定消息设置为 SEEN(读取)。

我试图找到一种方法来即时将模式从 READ_ONLY 更改为 READ_WRITE,但显然唯一的方法是关闭 Folder 并再次打开它.

我写了一个实现上述目标的重要方法,但我对这种情况真的很不满意。

如果我从一开始就以 READ_WRITE 的身份打开文件夹,邮件在我处理过程中会被标记为 READ,这不是我希望邮件被标记为的时间点READ.

看一下我写的方法,别傻笑

/**
 * @param message The message to be processed.
 * @param read True to mark as READ, False to mark as UNREAD.
 */
public static void markMessageAsRead(Message message, boolean read) {
        try {
            //Getting required variables
            Folder messageFolder = message.getFolder();
            int initialFolderMode = messageFolder.getMode();
            boolean initialFolderOpenState = messageFolder.isOpen();

            //If the folder is readonly then lets set it to readwrite
            if(initialFolderMode == Folder.READ_ONLY) {
                if(initialFolderOpenState) {
                    messageFolder.close(false);
                }

                messageFolder.open(Folder.READ_WRITE);
            }

            //Make sure folder is open (incase the above if didn't evaluate)
            if(!initialFolderOpenState) {
                messageFolder.open(Folder.READ_WRITE);
            }

            //Marking message as seen/unseen
            message.setFlag(Flags.Flag.SEEN, read);

            //Now lets revert the folder to it's state before it came here
            if(initialFolderOpenState) {
                if(!messageFolder.isOpen()) {
                    messageFolder.open(initialFolderMode);
                }
            } else {
                if(messageFolder.isOpen()) {
                    messageFolder.close(false);
                }
            }
        } catch (MessagingException e) {
            e.printStackTrace();
        }
}

有没有办法更巧妙地实现我想要的?

传统的解决方案是在不设置seen标志的情况下以读写模式打开文件夹,然后在想要获取数据时使用peek功能。查看的命令不设置 seen 标志。