Reading mails using IMAP and MailSystem.NET

In this article I will describe how you can use C# and MailSystem.NET to read mails from any IMAP source, including GMail.

MailSystem.NET is a Open Source, GNU Library General Public Licensed library for managing emails using SMTP, POP3, IMAP and many more protocols.

Download MailSystem.NET here.

I needed a component that will read mails from an IMAP source and import them into Sitecore. So I built this simple repository to get me all mails from a certain mailbox. Here is how I did it.

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.

About these ads

About Brian Pedersen

Developer at Pentia A/S since 2003. Have developed Web Applications using Sitecore Since Sitecore 4.1.
This entry was posted in .net, c# and tagged , , , , , . Bookmark the permalink.

36 Responses to Reading mails using IMAP and MailSystem.NET

  1. moyt says:

    How can I insert the dll to wpf?

  2. Paul says:

    Is there any way of using OAuth 2.0 with the MailSystem.NET in the code sample the username, password combination. For some applications however you are unlikely to have that combination.

  3. Ognjen says:

    Thank you very much. Your example works perfectly!!

  4. nagendra says:

    I am getting the following error.

    A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 173.194.70.108:993.
    Pls help me…!

  5. briancaos says:

    The message implies that there is no connection between your server and the IMAP server you are trying to connect to.
    For further information try this link: http://lmgtfy.com/?q=A+connection+attempt+failed+because+the+connected+party+did+not+properly+respond+after+a+period+of+time%2C+or+established+connection+failed+because+connected+host+has+failed+to+respond+

  6. hoang nguyen says:

    I need to get the contents of the mail.if used “email.BodyHtml.Text” then it automatically insert HTML code.I try “email.BodyText” but is not, please help me

  7. Paul Chapman says:

    hoang – try mail.BodyText.TextStripped instead of email.BodyText

  8. hoang nguyen says:

    a problem occurred when retrieving mail.eg: in gmail, mail sent to the Inbox, 2 mail has the same title, it will be classified in the same group.While it only took a second mail, mail for no gain.How do I fix this problem?

  9. Leena says:

    hi , your article is very good
    when i use your code, while connecting to our imap server, its give me following message:
    Unable to read data from the transport connection: An established connection was aborted by the software in your host machine.

    any idea, what can be problem

  10. briancaos says:

    This exception is thrown when the mail server you are requesting closes the connection. You need to ensure that you are requesting data from the correct port, and use SSL if needed.
    You should also ansure that your firewall and antivirus allows you to send AND receive data from the port you are requesting. McAfee antivirus for example is known for blocking mail send requests.

    Detecting network related issues is very difficult but tools like Fiddler (http://www.fiddler2.com), NetTool (http://nettool.sourceforge.net/) and especially Wireshark (http://www.wireshark.org/) is great tools for seeing what’s going on between your machine and the target host.

    The following articles gives you a more technical description of the issue:

    - http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_24942090.html
    - http://social.msdn.microsoft.com/Forums/da-DK/ncl/thread/29cabe89-db35-48ae-bc54-91b583eeed55

  11. nitin says:

    hey here UNSEEN is not working as it gives a error saying that
    “object reference not set ot an instance of object ” pointing out the
    line -”messagecollection messages=mails.SearchParse(searchphrase);
    can u please help me out it’s important part of my academic project please reply me soon thnks

  12. briancaos says:

    The “UNSEEN” is a mail server search phrase describing to the mail server that it must return all unread mails from the inbox.
    Not all mailservers uses this phrase. Please refer to the manual of your mailserver what phrase to use when looking for unread mails.

  13. Saif says:

    Hi,
    Nice article with good information. I have following query, if you can help?
    - Can we identify from Headers if email have attachments?
    I want to display attachment icon with email information same hotmail, gmail inbox.

    Thanks

  14. FYI – You can speed up your login by using Imap4Client.LoginFast() and then lazy load whatever you need.

  15. Eduardo says:

    I am getting the following error.

    “The remote certificate is invalid according to the validation procedure”,

    Where do i have to specify the certificate or is there a method which allows me to skip the validation. Can you please help me out

    Thanks in advance

  16. Krishna Varma says:

    how to send mails using this IMAP server url?

  17. Raveesh G S says:

    A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 173.194.70.108:993.

    same problem persist for me also while connection to gmail (imap.gmail.com)

  18. WGHS says:

    why this application dose not work in .net windows form application. it only works on web application

  19. vinodh says:

    hi sir,
    your sample worked perfectly for me.. thanks a lot.. can u pls let me know how to get mails from send folder..

  20. Bob Taylor says:

    Good stuff! Thanks for sharing.

  21. Samer says:

    Thank you for your effort.
    Would you please provide us with any book that contains tutorials about mailsystem.net

  22. Thanx on library.
    Is there a chance to count unread messages without setting flag on IMAP server readed.
    When I do mails.SearchParse(“UNSEEN”);
    All messages get status readed.

  23. Martin H says:

    Hello,
    I`ve got a question, the “UNSEEN” messages are not marked as “read” after i executet the programm. Do you know why this could happen ?

  24. Pooja says:

    Hi,

    I have a requirement to read link from the email body and go to the link. Is there any way in the library to get the link in email body or we need to manually scan the email body to get the link?

    Thanks
    Pooja

  25. EzLo says:

    Admin Hod:

    To count unread messages just use mailbox.messageCount and substract the ordinal position of the first unseen message in the mailbox using the property of the mailbox called “firstunseen”.

    Pooja:

    There is no way with MailSystem.net to determine a link within the body of a mail. You will have to parse it yourself unfortunately.

    vinodh:

    To open mails from the Sent mailbox, just need to select that mailbox instead of Inbox. However, different mail servers (gmail, yahoo, etc) name the “Sent” mailbox differently (for example its [Gmail]/Sent with gmail with an english account or [Gmail]/Enviados with a spanish account). To figure out the exact name, you can issue a “List” command, or you can debug and check the “allMailboxes” property of an already connected Imap4Client (which was connected by login method and not by fastLogin).

    Krishna Varma:

    IMAP is only for retrieval of mails, to send one you need to use Simple Message Transfer Protocol (SMTP) which has different host, port and commands (still supported with MailSystem.net though).

    Martin H:

    Unseen mails are mails that do not posess the “seen” flag. There isn’t a “not seen” flag. The way to adquire the Message object in MailSystem.net determines whether this flag is automatically eliminated or not. Use mailbox.Fetch.messageObject(…) to get the Message object and setting the “seen” flag, or use mailbox.Fetch.messageObjectPeek(…) to get the Message object without chaging the “seen” flag (so mails not seen will remain the same.
    If you use mailbox.searchPhrase(…), the server will always add the “seen” flag (there is a workaround this but its like bypassing MailSystem library, by issuing a Command with BODY.PEEK parameter).

    Hope this helps!

  26. hasanein says:

    give me error,the method is must return type( MailRepository
    )

  27. Manve says:

    How to Monitor mail server for incoming mails? Threading? or is there any inbuilt method to do it?

  28. Marc says:

    Hi, I tried this library with Gmail. Got a successful connection but it took ages to do the login even though it eventually succeeded.

  29. Suresh says:

    It works perfectly…. I would like to download the attachment…. how I should do it could you please help me….

  30. Timeternity says:

    Client.Login(login, password) => Client.LoginFast(login, password);
    It can resolve the very slow problem.

  31. Ameli says:

    How to import folders from one imap email client for C #?

  32. What the says:

    Where the [][] is your response (Response.Write(…)). What the [][] is Response? A tree? A potato?

  33. briancaos says:

    Yes it’s a tree. No, its a test function on an .aspx page from a web project. The Response.Write() is part of the HttpContext.Current. namespace.

  34. Thank you for this, i made a few additions. public class MailRepository
    {
    private Imap4Client _client;

    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 GetAllMails(string mailBox)
    {
    return GetMails(mailBox, “ALL”).Cast();
    }

    public IEnumerable GetAllTo(string mailBox, string emailFilter)
    {
    var test = GetMails(mailBox, string.Format(“TO ‘{0}’”, emailFilter)).Cast();
    return test;
    }

    public IEnumerable GetAllToFromDate(string mailBox, string emailFilter, DateTime date)
    {
    var dateFormatted = date.ToString(“dd-MMM-yyyy”);
    var test = GetMails(mailBox, string.Format(“TO ‘{0}’ NOT BEFORE {1}”, emailFilter, dateFormatted)).Cast();
    return test;
    }

    public IEnumerable GetMailsFromDate(string mailBox, DateTime date)
    {
    var dateFormatted = date.ToString(“dd-MMM-yyyy”);
    return GetMails(mailBox, string.Format(“NOT BEFORE {0}”, dateFormatted)).Cast();
    }

    public IEnumerable GetUnreadMails(string mailBox)
    {
    return GetMails(mailBox, “UNSEEN”).Cast();
    }

    public IEnumerable GetUnreadMailsFromDate(string mailBox, DateTime date)
    {
    var dateFormatted = date.ToString(“dd-MMM-yyyy”);
    return GetMails(mailBox, string.Format(“UNSEEN NOT BEFORE {0}”, dateFormatted)).Cast();
    }

    protected Imap4Client Client
    {
    get { return _client ?? (_client = new Imap4Client()); }
    }

    private MessageCollection GetMails(string mailBox, string searchPhrase)
    {
    Mailbox mails = Client.SelectMailbox(mailBox);
    MessageCollection messages = mails.SearchParse(searchPhrase);
    return messages;
    }
    }

  35. Diego says:

    Thaks, it works very fine. Compliments….

  36. Deepak says:

    “The handshake failed due to an unexpected packet format”. Error is coming while executing the application.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s