Search code examples
searchimap

Synchronize (Replicate) IMAP Messages


I have an imap account, (e.g. [email protected]) and I know many libraries with which I can connect and replicate messages back to my destination. I want to achieve following,

  1. First time, I want to download all messages (including sent folders), and when I download for the first time, I will save message with ID and UID locally in some database.
  2. Second time, I do not want to query downloaded messages, even though their read/unread status or any flag or deleted flag is changed or they are purged.
  3. Our aim is to download and sync every messages locally, once and only first time.

Now I know little about IMAP message that they have something called ID, UID and MessageID. ID is probably an offset in current folder, UID is numeric id in current account and MessageID is a unique string.

Now I want to know, what search I should use while querying folder, so that messages once downloaded, wont be returned back to me.

I am planning to use http://mailsystem.codeplex.com/ library, and it gives ability to Search with custom string and returns int array.

Assuming I have, a MaxID, and I want to only download messages which has ID or UID greater than MaxID. Which one should I use? UID or ID?


Solution

  • You should use the UID in combination with UIDVALIDITY. Both values are folder specific.

    There is an informational RFC that describes how IMAP clients should do synchronization (RFC-4549, section 4.3). The text recommends issuing the following two commands:

    tag1 UID FETCH <lastseenuid+1>:* <descriptors>
    tag2 UID FETCH 1:<lastseenuid> FLAGS
    

    The first command is used to fetch the required information for all unknown mails (without knowing how many mails there are). The second command is used to synchronize the flags for the already seen mails.

    AFAIK this method is widely used. Therefore, many IMAP servers contain optimizations in order to provide this information quickly. Typically, the network bandwidth is the limiting factor.