Posts Tagged ‘GMail’

Uploading emails using IMAP

Monday, January 4th, 2010

Uploading emails to the IMAP server is fairly easy with Mail.dll IMAP client

First we’ll use Mail.dll to upload an existing email in eml format to the IMAP server:

// C# code

using (Imap imap = new Imap())
{
    imap.Connect("server");

    imap.User = "user";
    imap.Password = "password";
    imap.Login();

    string eml = File.ReadAllText("email.eml");

    // The name of the folder depends on your IMAP server
    imap.UploadMessage("[Gmail]/Sent Mail", eml);

    imap.Close(true);
}
' VB.NET code

Using imap As New Imap()
	imap.Connect("server")

	imap.User = "user"
	imap.Password = "password"
	imap.Login()

	Dim eml As String = File.ReadAllText("email.eml")

	' The name of the folder depends on your IMAP server
	imap.UploadMessage("[Gmail]/Sent Mail", eml)

	imap.Close(True)
End Using

Second sample shows how to create new email message and upload it to IMAP server.

// C# code

using (Imap imap = new Imap())
{
    imap.Connect("server");

    imap.User = "user";
    imap.Password = "password";
    imap.Login();

    // Create new mail message
    SimpleMailMessageBuilder builder = new SimpleMailMessageBuilder();
    builder.Subject = "subject";
    builder.From.Add(new MailBox("alice@email.com", "Alice"));
    builder.To.Add(new MailBox("bob@email.com", "Bob"));
    builder.SetTextData("This is plain text email");

    // Upload
    // The name of the folder depends on your IMAP server
    imap.UploadMessage("[Gmail]/Sent Mail", builder.Create());

    imap.Close(true);
}
' VB.NET code

Using imap As New Imap()
	imap.Connect("server")

	imap.User = "user"
	imap.Password = "password"
	imap.Login()

	' Create new mail message
	Dim builder As New SimpleMailMessageBuilder()
	builder.Subject = "subject"
	builder.From.Add(New MailBox("alice@email.com", "Alice"))
	builder.[To].Add(New MailBox("bob@email.com", "Bob"))
	builder.SetTextData("This is plain text email")

	' Upload
	' The name of the folder depends on your IMAP server
	imap.UploadMessage("[Gmail]/Sent Mail", builder.Create())

	imap.Close(True)
End Using

Please note that only few IMAP servers are going to send the message to the actual recipients. Most servers will only upload the message without sending it.
You should use SMTP protocol for this.
You can download Mail.dll IMAP client here.

Download unseen emails from GMail

Friday, November 27th, 2009

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.User =  "your_email@gmail.com";
	imap.Password = "password";
	imap.Login();

	imap.SelectInbox();
	List<long> uids = imap.SearchFlag(Flag.Unseen);
	foreach (long uid in uids)
	{
		string eml = imap.GetMessageByUID(uid);
		ISimpleMailMessage mail = new SimpleMailMessageBuilder()
			.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.User = "your_email@gmail.com"
	imap.Password = "password"
	imap.Login()

	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 ISimpleMailMessage = New SimpleMailMessageBuilder()_
			.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