Search code examples
c#.netimapmailkitmimekit

MailKit/IMAP/QRESYNC: Updating cached HighestModSeq?


The following answer provides an excellent roadmap for using MailKit to leverage QRESYNC in a message sync implementation:

MailKit IMAP fetch only new, not downloaded messages

However, it does not cover the updating of the local cache with an updated HighestModSeq value. Specifically, how is the new value obtained? When is the appropriate time to obtain it? I assume it is expected to change after any MessagesVanished, CountChanged, or MessageFlagsChanged event fires. Any clarification on this point would be immensely appreciated.


Solution

  • However, it does not cover the updating of the local cache with an updated HighestModSeq value. Specifically, how is the new value obtained?

    After Open()ing the folder (no matter which overload you use), the ImapFolder.HighestModSeq property will be set.

    When is the appropriate time to obtain it?

    I would probably not immediately set the value on the cache after opening the folder until you've re-sync'd with the server. Once you've done that, though, I would update the cached value and try and keep it updated.

    I assume it is expected to change after any MessagesVanished, CountChanged, or MessageFlagsChanged event fires.

    There is a folder.HighestModSeqChanged event that you can listen for. This event gets emitted in the following cases:

    1. a HIGHESTMODSEQ resp-code is sent by the server (signifying that the value has changed)
    2. The user has called one of the Fetch() overloads and a MODSEQ value larger than the folder's current HighestModSeq value is encountered in the FETCH response.

    I've updated the other answer to add in logic for updating the cached HighestModSeq as well as the UidValidity values.