IMAP synchronization
Asked Answered
M

2

12

I'm implementing a IMAP client, and I have a problem with IMAP mailbox synchronization.

First, it is okay to fetch new mails from IMAP server, but I don't know how to find deleted messages from a mailbox.

Should I fetch all messages from the server and compare it with local data to synchronize?

Marko answered 31/3, 2012 at 13:35 Comment(0)
C
27

The usual approach is to execute the following two IMAP commands for each folder:

. EXAMINE "<foldername>"
. FETCH 1:* (UID FLAGS)

The first command selects a folder and returns the UIDVALIDITY of this folder. If this value matches the previously returned UIDVALIDITY for this folder, you can rely on the UIDs. The second command returns (at least) the UID and all FLAGS for each mail in the selected folder.

  • You should use the UID to detect which mails have been added or removed. Note that the content of an email can not be changed without also changing the UID.
  • In basic IMAP, the FLAGS are the only attributes that can be changed for an email. The flags contain information about read mails (\Seen) and deleted mails (\Deleted).

This approach is used by many IMAP clients, and most IMAP servers are optimized for them. The limiting factor is usually the available network bandwidth between client and server.

The following situations are a bit more complicated:

  • What should be done if UIDVALIDITY does not match? The IMAP specification requires that servers do their best to avoid unnecessary changes to this value.
  • Should there be an optimization for moved mails (actually copied mails)? In basic IMAP, you can not detect that an email is a copy of another email - regardless whether the email in the source folder still exists or whether it has already been deleted and expunged.
Chaulmoogra answered 1/4, 2012 at 18:55 Comment(1)
The UIDPLUS extension can help with moved/copied emails. Another method to discover deleted messages is to use UID SEARCH: EXAMINE "folder" then UID SEARCH 1:*. This will provide a list of all the UIDs currently in the mailbox, which can be compared to your local cache.Therontheropod
C
15

Maybe too late for your app, but you can find a complete IMAP sync strategy in this RFC: https://www.rfc-editor.org/rfc/rfc4549 !

Courthouse answered 12/8, 2014 at 7:35 Comment(1)
tl;dr. Is there a summary anywhere?Hallvard

© 2022 - 2024 — McMap. All rights reserved.