0 votes

Hi,
I need to do the following:
1. initially download all my emails from gmail (all folders, all flags)
2. save into a sql server table
3. on a regular basis, download new emails which do not already exist in my sql server table (all folders, all flags)

Pls assist me with code sample.

Thank you

by (200 points)
This is quite a task. What have you tried, what you are having problems with?
Actually I have written something which is working fine,
only problem is im getting 10,000 records, while Gmail says i have 20,000 records.
Thanks
I'd expect something quite opposite as Gmail reports the number of conversations you have (and conversation can contain many email messages).

1 Answer

0 votes

I can recommend using IMAP.

If you are targeting Gmail only, use its 'All Mail' folder (use CommonFolders class).

Download most common email information, including Gmail flags first. You can use GetMessageInfoByUID for that:

https://www.limilabs.com/blog/get-email-information-from-imap-fast

This way you can find which uids are already in your SQL database and mark those that aren't to be downloaded. Also you don't need to traverse entire folder structure (folders are in fact labels in Gmail and all messages are in stored in 'All Mail' folder).

Then use GetMessageByUID to download actual messages (this includes attachments).

Articles you may find useful:
https://www.limilabs.com/blog/get-new-emails-using-imap

https://www.limilabs.com/blog/download-unseen-emails-from-gmail-using-imap-video

https://www.limilabs.com/blog/unique-id-in-imap-protocol

by (297k points)
edited by
...