Download emails from Gmail

The task is quite easy with Mail.dll IMAP client.
IMAP protocol unlike POP3 stores the unseen information on the server. So all we need to do is connect via SSL, search for unseen emails, and download them. Mail.dll will do all the hard work.
First thing to remember is to enable IMAP in Gmail, second is to use ConnectSSL method, as Gmail allows only secure SSL connections.
We’ll use Imap.Search(Flag.Unseen) method to download unique ids of all unseen email messages in our inbox. Finally the sample shows how to download such emails and parse them using MailBuilder class. After this it is easy to access any email data like: Subject, Date, From and To collections, plain-text and HTML versions of the email. Also all Attachments are downloaded and stored in IMail.Attachments collection.
// C# code:
using(Imap imap = new Imap())
{
imap.ConnectSSL("imap.gmail.com");
imap.Login("pat@gmail.com", "password");
imap.SelectInbox();
List<long> uids = imap.Search(Flag.Unseen);
foreach (long uid in uids)
{
string eml = imap.GetMessageByUID(uid);
IMail mail = new MailBuilder().CreateFromEml(eml);
Console.WriteLine(mail.Subject);
Console.WriteLine(mail.Text);
}
imap.Close();
}
' VB.NET code:
Using imap As New Imap()
imap.ConnectSSL("imap.gmail.com")
imap.Login("pat@gmail.com", "password")
imap.SelectInbox()
Dim uids As List(Of Long) = imap.Search(Flag.Unseen)
For Each uid As Long In uids
Dim eml As String = imap.GetMessageByUID(uid)
Dim mail As IMail = New MailBuilder().CreateFromEml(eml)
Console.WriteLine(mail.Subject)
Console.WriteLine(mail.Text)
Next
imap.Close()
End Using
If you like the idea of such simple Gmail access just give it a try for yourself and download Mail.dll .NET IMAP component.
December 1st, 2009 at 17:54
[...] Mail.dll has of course IMAP support [...]
November 15th, 2010 at 17:14
[...] articles can help you with that: How to enable POP3 for Gmail How to enable IMAP for Gmail How to enable IMAP for [...]
October 21st, 2011 at 19:55
[...] Mail.dll contains ready to use IMAP client. Take a look at the IMAP sample. [...]
August 4th, 2012 at 16:15
Id like to know how to keep messages in my GMAIL INBOX marked as UNREAD, using this procedure.
August 6th, 2012 at 08:41
@Matias,
You can use PeekMessageByUID method if you don’t want the message to be marked as seen after download.
August 27th, 2012 at 21:11
You may know the old read mail in GMAIL? I have to compare all the emails?
or I can run a query?
August 28th, 2012 at 11:23
@David
You can search for certain flags (seen, unseen),
you may also search using Gmail’s search syntax.
January 28th, 2013 at 13:23
How can I check my Gmail inbox?
January 29th, 2013 at 09:06
@Peace
What is your problem exactly?
April 9th, 2013 at 18:16
Does it is possible to mark the unseen mail as seen in gmail. That is once the unseen mail will be read then and then it will mark in gmail server. So that from next time always the unseen mails will be received.
April 9th, 2013 at 19:09
@Kaushik
You can either peek email on the server, by using one of the PeekXXX methods (e.g. PeekMessageByUID), as they don’t change the state of the message on the server
-or-
you can mark message seen or unseen explicitly.