Expunge 导致 Message 对象在被删除的消息之后重新编号

Expunge causes the renumbering of Message objects subsequent to the expunged messages

来自 javamail MailFolder class doc 我读到这个:

Note that a Message's message number can change within a session if the containing Folder is expunged using the expunge method. Clients that use message numbers as references to messages should be aware of this and should be prepared to deal with situation (probably by flushing out existing message number references and reloading them). Because of this complexity, it is better for clients to use Message objects as references to messages, rather than message numbers. Expunged Message objects still have to be pruned, but other Message objects in that folder are not affected by the expunge.

我想知道如果 Message 对象发生变化,我如何使用 Message 对象作为对消息的引用,我的意思是即使我在删除之前存储 Message 对象引用,一旦 MessageCount REMOVED 事件到来,对象引用就不同于之前存的那个

我问这个是因为我在使用消息编号作为已删除消息的参考时遇到了问题...所以我正在考虑为每个 REMOVED 事件完全重建 Map of。 我不太喜欢这种方法,因为我认为它在巨大的邮箱中可能会很慢……但我看不到使用 messageNumber 的好处……

谢谢

消息对象引用在删除消息时不会改变。引用消息编号 17 的消息对象现在可能引用消息编号 15,但它在服务器上将是相同的 消息

显然,消息对象引用仅在 JVM 处于 运行 且文件夹处于打开状态时才有效。如果您需要消息的长期参考,则需要了解 IMAP UID;看到 UIDFolder interface and the IMAP RFC.