Skip to content
222

ImapX – free for use .NET library

ImapX is an modern .NET 2.0 library for management of IMAP folders, supports SSL Connection and different IMAP formats, such as Gmail IMAP, AOL IMAP

Here is simple example of usage:

      ImapX.ImapClient client = new ImapX.ImapClient("imap.gmail.com", 993, true);
      bool result = false;

      result = client.Connection();
      if (result)
        Console.WriteLine("@Connected");

      result = client.LogIn(userName, userPassword);
      if (result)
      {
        Console.WriteLine("@Logged in");
      }
      //Folder Collection
      ImapX.FolderCollection folders = client.Folders;
      //Create folder
      client.CreateFolder("NEW FOLDER", false);
      //Message collection 1st option
      ImapX.MessageCollection messages = client.Folders["INBOX"].Search("ALL", true); //true - means all message parts will be received from server

      //2nd option
      foreach (ImapX.Message m in client.Folders["INBOX"].Messages)
      {
        m.Process();
        List attachments = m.Attachments;
        string textBody = m.TextBody.TextData;
        string htmlBody = m.HtmlBody.TextData;
      }

      ImapX.Message msg = new ImapX.Message();
      msg.Subject = "hello yahoo";
      msg.From.Add(new ImapX.MailAddress(fromUserName, fromUserEmail));
      msg.To.Add(new ImapX.MailAddress(toUserName, toUserEmail));
      msg.HtmlBody.ContentType = "text/html";
      msg.HtmlBody.TextData = "it is test message";

      ImapX.Attachment atach = new ImapX.Attachment();
      atach.FileName = attachmentPath;
      msg.Attachments.Add(atach);
      if (client.Folders["INBOX"].AppendMessage(msg))
      {
        Console.WriteLine("message has been appended to INBOX");
      }

Feature list supported by library:

  • Folder management
  • Message management
  • Message saving in IMAP folder (like Drafts on Gmail)
  • SSL
  • Custom IMAP server implementations (like Google and AOL)
  • Full IMAP Search compatible

Features to be implemented during near future:

  • Offline message storage/synchronization

In ZIP archive you will find actual dll to use and IMAP Search reference document, sorry no documentation at this moment.

Hopefully it will be usefull.

Feb 24, 2010 update:

We have fixed following issues:

  1. now we correctly support message flags (“DELETED”, “SEEN”, “ANSWERED”, “DRAFT”, “RECENT”) and custom flags
  2. correctly handling spaces in passwords (need to be tested more, please contact us if you’ll find an issues there)

There are code sample which allows to set message flags:

ImapX.MessageCollection messages = client.Folders["INBOX"].SubFolder["NEWFOLDER"].Search("ALL");
//get list of current message flags
List allFlags = messages[1].Flags;
//set fllag array of flags are in ImapFlags constants
messages[1].SetFlag(ImapX.ImapFlags.SEEN);

Feb 25, 2010 update:
Today we have released version 1.0.5 of the library

Fixed issues:

  1. now correctly handling different types of subjects from IMAP

March 4, 2010 update:
Today we have released version 1.0.6 of the library with a great performance improvements (up to 4 time faster)

Fixed issues:

  1. Now it’s possible to search for messages without auto-filling the body, header and other message properties. Added new bool parameter for Search method which indicates to fill properties or no

Here is code example

//result messages  + process()
ImapX.MessageCollection messages = client.Folders["G01"].Search("ALL", true);
//result empty messages
ImapX.MessageCollection messages = client.Folders["G01"].Search("ALL", false);
//fill message header info
messages[1].ProcessHeader();
//fill message flags info
messages[1].ProcessFlags();
//fill message body info
messages[1].ProcessBody();
//fill all message info
messages[1].Process();

March 4, 2010 update:
We have updated library to 1.0.6.1 version. Fixed some minor bugs. Thanx to Dalibor.
March 25, 2010 update:
We have updated library to 1.0.6.3 version. Fixed some more minor bugs.
March 26, 2010 update:
We have updated library to 1.0.6.4 version. Added IsDebug property to ImapX.ImapClient

client.isDebug  = true; // with this option , all requests and responses are print to Console window

April 14, 2010 update:
We have updated library to 1.1 version. Fixed bugs with message processing, users faced in previous releases.
Please note: if your are facing any problems with message body or if message has no content-type, you may find all body parts in BodyParts collection:

   List<MessageContent> bodyParts = msg.BodyParts;
   //get body :
   string body = bodyParts[index].ContentStream;

Some users facing problems with different Subject/Body encodings. Please note – ImapX itself doesn’t decode subject/body automatically. For instance if you have subject encoded to BASE64 using UTF8 charset you have to do following techniques:

   //preparing subject to be sent
   string codedSubject = "=?UTF-8?B?" + Convert.ToBase64String(Encoding.UTF8.GetBytes(subject)) + "?=";
   //decoding subject from received message
   string decodedSubject = Encoding.UTF8.GetString(Convert.FromBase64String(CodedSubject.Split('?')[4]));
   //decoding body from Base64/UTF8 :
   string decodedBody = Encoding.UTF8.GetString(Convert.FromBase64String(msg.HtmlBody.TextData));

Thanks to all of users of ImapX. And thank you for you comments, your responses help us to develop better IMAP library.

April 27, 2010 update:
We have updated body processing, ImapX refused to consume some bodies produced by Outlook 2007. It’s fixed in 1.1.0.1 version

May 12, 2010 update:
Updated:

  • Message parsers updated
  • Fixed problems with special characters in folder names
  • Added body part properties: BoundaryNames and PartHeaders
    Example:

    MessageContent body = message.HtmlBody;
    body.BoundaryNames // name of boundary of this part
    body.PartHeaders // Collection headers of this part

July 7, 2010 update:
Updated:

  • Message parsers updated
  • Fixed problems with marking message as seen during processing
  • Fixed attachments processing

If you’ll face problems with a message parsing then please use method GetAsString() of class Message. It will allow you to use custom parsers to parse a message

Download

Comments
  • David Lozzi 02/18/2011 at 01:28

    Could you provide a strong named version of the DLL? I can’t use it in my project without it being strong named. I tried to disassemble and then sign and reassemble but the project won’t compile with it.

    Thanks,
    David

  • OliverSleep 02/23/2011 at 11:48

    Hi,
    Thks for your library, It works very well :) .

    Is there any issue to change the timeout of the different connection methods ?

  • Baris Gulecyuz 02/27/2011 at 21:24

    Hi,
    I want to read emails from gmail and I want to get all attachment from message and I want to save this attachments to my server.Is that possible with this library?and is there sample code for this.
    thanks

  • sepdau 03/24/2011 at 10:48

    how to get all name of folder in gmail?

  • Jame Backstrom 04/10/2011 at 14:32

    Nice.

  • Dulce 04/14/2011 at 22:06

    Hi thanks for this library, so i am trying read the email with special characters as the word “Administración” and return this “Administraci=C3=B3n”,
    I don’t understand how use the encoding

    I use the spanish lenguage
    Please help me

    Thanks!

  • Corrine Speller 04/20/2011 at 02:51

    Where is your rss? I cant find it

  • Goku 04/24/2011 at 17:33

    hey how can i mark email as READ or UNREAD ? plz help !

  • eddie 04/27/2011 at 01:28

    hi, thanks for such an amazing tool..
    I have a question.. how can I access yahoo inbox with it?
    I keep getting error “Bad or not correct Path” here is my code so far..

    ImapX.ImapClient client = new ImapX.ImapClient(“imap.mail.yahoo.com”, 993, true);
    bool result = false;

    result = client.Connection();
    if (result)
    textBox1.Text = “Connected\r\n”;

    client._client.SendCommand(“ID (\”GUID\” \”1\”)”);

    result = client.LogIn(“xxxx@yahoo.co.uk”, “xxxx”);
    if (result)
    {
    textBox1.Text = textBox1.Text + “Logged in\r\n”;
    }
    ImapX.MessageCollection messages = client.Folders["Inbox"].Search(“ALL”, true);
    for (int s = 0; s < messages.Count; s++)
    {
    textBox1.Text = textBox1.Text + messages[s].TextBody.TextData + "\r\n";
    }
    still not sure what I'm doing wrong….

  • sepdau 05/04/2011 at 20:03

    Goku
    hey how can i mark email as READ or UNREAD ? plz help !

    I have a same question

  • Gothrek 05/09/2011 at 11:54

    Hi,
    when i try to getfolders i receive an error about matrix limits
    my connection is true, my login is sucessfull if i want simple know how many dirs the mailbox have client.getfolders.count i receive the message
    (i use a private imap service not google)

    tks for help

  • Gothrek 05/09/2011 at 12:04

    naturally i try to use this before getfolders:
    Dim cartelle As ImapX.FolderCollection = client.Folders
    and i receive the same exception error.
    I read in some post about this error and u say download the new version. I think have the last version.

    tks for help

  • Tibbie 05/15/2011 at 16:22

    BION I’m imperssed! Cool post!

  • Nikita 05/17/2011 at 18:37

    Hi. First of all I want to say thanks for your library.
    I have a few questions:
    - How can I get all unreaded messages from some folder?
    This code returns NullReferenceException
    Folder folder = client.Folders["SomeLabel"];
    MessageCollection mc = folder.CheckNewMessage(true);
    - How can I mark message as “read”? The code below doesnt work.
    mes.SetFlag(ImapFlags.SEEN);
    - What can I put into “path” param in the Folder.Search method exept “ALL”?

  • Nikita 05/17/2011 at 18:50

    My apologies. mes.SetFlag(ImapFlags.SEEN) works. Please remove this question from my last post

  • Alex 05/28/2011 at 02:59

    I have the same problem..

    Scott Sommerfeldt
    this mist be simple but I am missing something here.
    All I want to do is Login to a gmail account … iterate through the Unread emails and Delete them after they have been processed. Is this possible. here is my code… I cannot get the message to delete … PLEASE HELP
    //===========================================
    ImapX.ImapClient client = new ImapX.ImapClient(“imap.gmail.com”, 993, true);
    client.IsDebug = true;
    bool result = false;
    result = client.Connection();
    if (result)
    Console.WriteLine(“@Connected”);
    result = client.LogIn(“XXXMyGMAILADDRESS@gmail.com”, “XXXMYPASSWORD”);
    if (result)
    {
    Console.WriteLine(“@Logged in”);
    }
    //==========================================================
    var emailsInInbox = client.Folders["INBOX"].Messages.ToList();
    //Process the required details (otherwise sorting by dateis not possible)
    emailsInInbox.ForEach(mail => mail.ProcessFlags());
    emailsInInbox.ForEach(mail => mail.ProcessHeader());
    //Remove all messages which are marked as SEEN
    emailsInInbox = emailsInInbox.Where(message => !message.Flags.Contains(ImapFlags.SEEN)).ToList();
    foreach (ImapX.Message m in emailsInInbox)
    {
    m.Process();
    //List attachments = m.Attachments;
    string textBody = m.TextBody.TextData;
    string htmlBody = m.HtmlBody.TextData;
    // Parse and process the HTML Body
    // Mark the Email as Seen
    // Note this doesn’t work when debugging it says the Inbox is Read-only
    m.SetFlag(ImapX.ImapFlags.DELETED);
    }
    //Close the client connection
    client.LogOut();

  • Alex 05/28/2011 at 03:00

    I have the same problem..
    Any ideas how to fix this???

    Qnavry
    I am using the Code below to read unread messages and then process them(Save to a HD) if they have an attachment. This works fine if the email is sent in Plain Text format, but if it is HTML it does not show the attachment. Any ideas how to fix this?
    Thanks
    EmailConnector msg = new EmailConnector();
    msg.Connect(“xxx@xxx.com”,”pwd”);
    ImapX.MessageCollection messages = msg.GetNewMessages(“INBOX”);
    for(int i = 0;i < messages.Count;i++)
    {
    CommonLib.Message email = new CommonLib.Message();
    //messages[i].ProcessBody();
    email.Subject = messages[i].Subject.ToString();
    email.Body = messages[i].TextBody.TextData;
    email.Date = messages[i].Date;
    email.From = messages[i].From.ToString();
    for (int x = 0; x < messages[i].Attachments.Count; x++)
    {
    email.AttachmentFileName = messages[i].Attachments[x].FileName.ToString().Remove(messages[i].Attachments[x].FileName.ToString().IndexOf(“;”) -1);
    email.Attachment = messages[i].Attachments[x].FileData;
    ProcessRules(email);
    }

  • mohammad iliyash 07/18/2011 at 14:01

    Hello,
    I want to use IMAPX dll For business use.If need licence to use this DLL or it is free to use.

  • Post a comment
    You must be logged in to comment. Log in
1 ... 3 4 5
Trackbacks