I want to synch gmail labels in my imap application. I am syncing gmail using All Mails. After syncing one message in imap if user changes the label of IMAP. It will not automa开发者_Python百科tically comes as updated message. I am looking for best option to synch gmail labels.
What I thought is
- Either sync folder based.
- Checking labels for each message in gmail from starting to current stage.
but I don't think these two will be good option. What will be best option to sync gmail labels ?
For me following worked best:
- Only read messages from "All Mail"
- For every message whose labels you need to update, search all IMAP folders for its Gmail-Message-ID (or Thread-ID). You can also do this for multiple messages at a time.
- When a message is found in a folder, label it with the folder's name.
First: use only "All Mail" folder. Second: it is possible to get Gmail labels for many messages at once:
UID FETCH 1,2,3 (UID X-GM-LABELS)
If you are using .NET you can use this library (commercial component I created): http://www.limilabs.com/blog/get-gmail-labels-for-specified-messages
It should be quite fast, then its a matter of comparing the lists and using +X-GM-LABELS to add or -X-GM-LABELS to remove a label.
精彩评论