Download unseen emails from GMail

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 conect via SSL, search for unseen emails, and download them. Mail.dll will do all the hard work.

First we need to make sure that IMAP access is enabled for your GMail account.

  1. Go to GMail settings:

    gmail_settings

  2. Then select ‘Forwarding and POP/IMAP’ tab:

    gmail_settings_tab

  3. And finally Check ‘Enable IMAP’ in ‘IMAP Access’ section:

    gmail_settings_imap

  4. Now you are able to connect to your GMail account with Mail.dll IMAP client.

Remember that GMail only allows secure SSL connections so we need to use ConnectSSL method. We’ll also use Imap.SearchFlag(Flag.Unseen) to list the ids of unseen email messages.

// C# code:

using(Imap imap = new Imap())
{
	imap.ConnectSSL("imap.gmail.com");

	imap.Login("your_email@gmail.com", "password");

	imap.SelectInbox();
	List<long> uids = imap.SearchFlag(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.TextDataString);
	}
	imap.Close(true);
}
' VB.NET code:

Using imap As New Imap()
	imap.ConnectSSL("imap.gmail.com")
	imap.Login("your_email@gmail.com", "password")

	imap.SelectInbox()
	Dim uids As List(Of Long) = imap.SearchFlag(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.TextDataString)
	Next
	imap.Close(True)
End Using

If you like the idea of such simple GMail access just give it a try for yourself and download it at: Mail.dll .NET email component

Tags: , ,

One Response to “Download unseen emails from GMail”

  1. Lesnikowski blog » Blog Archive » Download emails from GMail via POP3 Says:

    [...] Mail.dll has of course IMAP support [...]

Leave a Reply