http://briancaos.wordpress.com/2012/04/24/reading-mails-using-imap-and-mailsystem-net/
how you can use C# and MailSystem.NET to read mails from any IMAP source, including GMail?
First you need to copy ActiveUp.Net.Common.dll and ActiveUp.Net.Imap4.dll from the MailSystem.NET package to your /bin/ folder.
Reading from MailSystem.NET is pretty straight forward:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ActiveUp.Net.Mail;
namespace PT.MailIntegration.IMAP
{
public class MailRepository
{
private Imap4Client _client = null;
public MailRepository(string mailServer, int port, bool ssl, string login, string password)
{
if (ssl)
Client.ConnectSsl(mailServer, port);
else
Client.Connect(mailServer, port);
Client.Login(login, password);
}
public IEnumerable<Message> GetAllMails(string mailBox)
{
return GetMails(mailBox, "ALL").Cast<Message>();
}
public IEnumerable<Message> GetUnreadMails(string mailBox)
{
return GetMails(mailBox, "UNSEEN").Cast<Message>();
}
protected Imap4Client Client
{
get
{
if (_client == null)
_client = new Imap4Client();
return _client;
}
}
private MessageCollection GetMails(string mailBox, string searchPhrase)
{
Mailbox mails = Client.SelectMailbox(mailBox);
MessageCollection messages = mails.SearchParse(searchPhrase);
return messages;
}
}
}
This repository can now be used to read all mails or all unread mails from a mailbox. The constructor takes the mail server name, port and SSL information, as well as the email address and password of the account to read from. GetAllMails() read all mails from the specified mailbox, while GetUnreadMails() reads all unread mails. Both functions returns a list of Message objects. A Message is the complete email in one object.
To read from a GMail mailbox you need to connect using SSL on port 993:
using ActiveUp.Net.Mail;
using PT.MailIntegration.IMAP;
protected void TestMail()
{
MailRepository rep = new MailRepository("imap.gmail.com", 993, true, @"mailaddress@gmail.com", "password");
foreach (Message email in rep.GetUnreadMails("Inbox"))
{
Response.Write(string.Format("<p>{0}: {1}</p><p>{2}</p>", email.From, email.Subject, email.BodyHtml.Text));
if (email.Attachments.Count > 0)
{
foreach (MimePart attachment in email.Attachments)
{
Response.Write(string.Format("<p>Attachment: {0} {1}</p>", attachment.ContentName, attachment.ContentType.MimeType));
}
}
}
}
Other IMAP mail servers might not need SSL, or requires another port number.
When calling GetUnreadMails(), mails will be marked as read afterwards.
Please note that reading using IMAP (at lest from GMail accounts) is very slow. Don’t be surprised if it takes up to a minute to get 30 mails from a mailbox.