Is there a built-in method to access an Imap server (with SSL) in C# or is there a good free library?
-
3duplicate: http://stackoverflow.com/questions/545724/using-c-net-librarires-to-check-for-imap-messages-from-gmail-servers – Mauricio Scheffer Oct 05 '09 at 13:44
-
7Nope. The link you posted is related to IMAP specifically for Gmail. This post, however, is not. – Oct 13 '10 at 09:15
-
77Please be more careful when saying/marking questions as duplicates. I'm sick of seeing "duplicate: [link] everywhere. A lot of the time the other links fail to answer what the "Duplicator" wants to know anyway, whether it's the same question or not! – βӔḺṪẶⱫŌŔ Apr 12 '11 at 08:10
-
8Question closed so I can't add this as an answer: https://github.com/jstedfast/MailKit seems to be a good option and an active project. – Rory Nov 25 '15 at 00:51
-
6Marking useful questions as not constructive is indeed not constructive... – Robert J Jul 21 '17 at 10:25
-
2Police state. Thanks for this question - it was super constructive and solved my issue. StackOverflow seems to be short on answers for this pertinent question. – Savage Jul 23 '18 at 16:11
-
1https://github.com/jstedfast/MailKit - Open Source has millions of NuGet downloads – Piotr Kula Jun 18 '20 at 23:18
6 Answers
I've been searching for an IMAP solution for a while now, and after trying quite a few, I'm going with AE.Net.Mail.
You can download the code by going to the Code tab and click the small 'Download' icon. As the author does not provide any pre-built downloads, you must compile it yourself. (I believe you can get it through NuGet though). There is no longer a .dll in the bin/ folder.
There is no documentation, which I consider a downside, but I was able to whip this up by looking at the source code (yay for open source!) and using Intellisense. The below code connects specifically to Gmail's IMAP server:
// Connect to the IMAP server. The 'true' parameter specifies to use SSL
// which is important (for Gmail at least)
ImapClient ic = new ImapClient("imap.gmail.com", "name@gmail.com", "pass",
ImapClient.AuthMethods.Login, 993, true);
// Select a mailbox. Case-insensitive
ic.SelectMailbox("INBOX");
Console.WriteLine(ic.GetMessageCount());
// Get the first *11* messages. 0 is the first message;
// and it also includes the 10th message, which is really the eleventh ;)
// MailMessage represents, well, a message in your mailbox
MailMessage[] mm = ic.GetMessages(0, 10);
foreach (MailMessage m in mm)
{
Console.WriteLine(m.Subject);
}
// Probably wiser to use a using statement
ic.Dispose();
Make sure you checkout the Github page for the newest version and some better code examples.

- 6,975
- 11
- 53
- 62
-
1
-
4@jase - You're right! I'm not sure what the problem is exactly (seems like the properties file?) but I was able to get it to compile by copying all relevant files into a new class project. Whatever the case, I built the libraries for you and so you can add them as .dll references to your project. http://dl.dropbox.com/u/8037514/AE.Net.Mail.zip Just as a disclaimer, this comes with whatever warranties and copyrights the original project comes with, and I'm not responsible if it blows up, etc etc. Good luck! – Dominic K Aug 06 '11 at 03:39
-
1
-
Just want to say that it works nicely, it's available on nuget, and the author provide very good support! – VinnyG Nov 02 '11 at 17:33
-
4+1 I just downloaded and compiled the latest commit of *AE.Net.Mail* in VS2010, and it worked perfectly. I had a **much** better experience than with ImapX, thanks for the tip. The code [here](http://www.woodoweb.com/index_files/archive-oct-2011.html) gave me a good jumpstart. – D'Arcy Rittich Nov 29 '11 at 14:33
-
This saved me so much time, thanks! You can get it from nugget for an easy install. – TheGateKeeper Feb 29 '12 at 19:16
-
This totally worked. I got the library using NuGet. DMan, you trully are The Man. – Pedro Jun 21 '12 at 21:34
-
@Pedro - Glad to have helped, but of course, all credits go to Andy Edinborough who made the library! – Dominic K Jun 22 '12 at 23:13
-
1
-
1
-
It does not build if you build the whole solution; but if you just build the class library project (do not build tests) it works just fine – roman m Oct 04 '13 at 00:48
-
Does this support oAuth 2 i.e. sending a token instead of passing password? – user1166905 Oct 29 '13 at 21:26
-
8why do people go and write great things like this and then include basically no documentation? I don't understand – Simon_Weaver Jul 04 '14 at 23:04
-
1watch out for one (in my opinion) bad design decision with `GetMessage` and that is that the `hasSeen` parameter defaults to true so it will mark all messages as read. if you're reading emails from a mailbox that a human is also reading you probably don't want this – Simon_Weaver Jul 05 '14 at 19:35
-
There is nuget package for this. Check in the GitHub page https://github.com/andyedinborough/aenetmail – Tejasvi Hegde Nov 07 '15 at 08:49
-
3It works but in newer versions the constructor should look like this: ImapClient ic = new ImapClient("imap.gmail.com", "name@gmail.com", "pass", AuthMethods.Login, 993, true); – Ogglas Dec 18 '15 at 11:09
-
Anyone knows how i can get idea about thread , I want show message in thread can i ? – Kaushik Thanki Jan 28 '16 at 06:55
-
Alternatively, you can download AE.Net.Mail from Nuget: PM> Install-Package AE.Net.Mail. IMap is available in the package – Venugopal M Oct 20 '16 at 11:45
-
-
1I think AE.Net.Mail is to buggy, could not get headers through imap in some cases for instance. Check out https://github.com/jstedfast/MailKit instead. – Ogglas Jan 24 '17 at 14:56
-
Here's the NuGet package: https://www.nuget.org/packages/AE.Net.Mail/ – Auri Rahimzadeh Jul 18 '17 at 13:21
In the hope that it will be useful to some, you may want to check out my go at it:
While there are a couple of good and well-documented IMAP libraries for .NET available, none of them are free for personal, let alone commercial use...and I was just not all that satisfied with the mostly abandoned free alternatives I found.
S22.Imap supports IMAP IDLE notifications as well as SSL and partial message fetching. I have put some effort into producing documentation and keeping it up to date, because with the projects I found, documentation was often sparse or non-existent.
Feel free to give it a try and let me know if you run into any issues!

- 22,600
- 28
- 79
- 90

- 359
- 3
- 2
-
1Tried AE.Net.Mail first and found S22.Imap to be better, both on documentation and on features I have needed so far. – angularsen Sep 25 '13 at 23:12
-
I liked to concept of S22.Imap. But What I am missing is accessing via index or message number. – Tejasvi Hegde Nov 07 '15 at 08:57
-
300 Cheers for S22.Imap. Simple Efficient and Useful in real world scenarios. The .Search() i sbrilliant – Vishnoo Rath Dec 01 '15 at 18:20
-
How can I keep synchronization for my mailbox with IMAP using this library? – JackXandar Nov 14 '16 at 08:48
-
1Any ideas why I'd get an InvalidCredentialsException with a message of "NO [WEBALERT some url] Web login required" when trying to login? IMAP is enabled on the Gmail account. Ideas? – Auri Rahimzadeh Jul 18 '17 at 14:22
-
1Got going in no time at all. I like that it uses the default .Net mail classes too. – Savage Jul 23 '18 at 16:09
-
There is no .NET framework support for IMAP. You'll need to use some 3rd party component.
Try https://www.limilabs.com/mail, it's very affordable and easy to use, it also supports SSL:
using(Imap imap = new Imap())
{
imap.ConnectSSL("imap.company.com");
imap.Login("user", "password");
imap.SelectInbox();
List<long> uids = imap.SearchFlag(Flag.Unseen);
foreach (long uid in uids)
{
string eml = imap.GetMessageByUID(uid);
IMail message = new MailBuilder()
.CreateFromEml(eml);
Console.WriteLine(message.Subject);
Console.WriteLine(message.TextDataString);
}
imap.Close(true);
}
Please note that this is a commercial product I've created.
You can download it here: https://www.limilabs.com/mail.

- 6,264
- 4
- 38
- 42
-
3
-
Does it support passing a token using oAuth 2 instead of passing password? – user1166905 Oct 29 '13 at 21:27
-
It does support OAuth2: http://www.limilabs.com/blog/oauth2-gmail-imap-web-applications – Pawel Lesnikowski Nov 22 '13 at 11:30
-
2Great component, works like a charm. Tested the trial version (almost fully functional) for a couple of weeks then bought a license. – Alex from Jitbit Jan 14 '14 at 20:32
-
1This is a great component indeed. Unlike all the open-source stuff we tried, this one is actually alive and up-to-date (I'm writing this in 2016). Thank you @PawelLesnikowski PS. I'm not affiliated in any way. – jazzcat Nov 18 '16 at 09:49
-
MailSystem.NET contains all your need for IMAP4. It's free & open source.
(I'm involved in the project)

- 9,597
- 8
- 59
- 85
-
2the project is active ?? latest version dated: Mon Dec 14 2009 at 9:00 AM – Kiquenet Nov 25 '10 at 10:33
-
1There is not so much feature to add today as most needs are already covered. Only bug fixes and small improvements are commited when they are submitted. But there are not so many, the library is very robust. – Nov 25 '10 at 10:55
-
-
If you are using POP3 or IMAP4 why not? If you want to use webdav, you have to look for another library. – Nov 25 '10 at 11:28
-
4This project seems to be dead. Does not work with IMAP4 properly, no updates and no answers from authors – Denis Vuyka Feb 24 '11 at 09:38
-
@Denis: ask to the other users http://mailsystem.codeplex.com/discussions because the open source version is not supported by the authors – Feb 24 '11 at 13:16
-
-
6@alhambraeidos and @Denis are correct. The project is practically dead. The samples do not build, out of the box, in either VS 2008 or 2010. I upconverted them then had to wade through 16 reference issues. The documentation is out of date and the CodePlex page sit unanswered for any issues/comments in the past year or more. I gave up and ended up purchasing Mail.dll (no, I'm not affiliated with them). – sohtimsso1970 Jun 28 '11 at 18:09
-
Not only project is dead, but project is not coded correctly, instead of using RegEx or little better way of parsing, String.IndexOf with hardcoded literals break the normal functionality and is less extensible as well. – Akash Kava May 22 '12 at 11:09
-
@AkashKava: standard parsing was prefered to RegEx for obvious performances reasons. – May 22 '12 at 11:15
-
I know, parsing is very complex, but better way would be to either incorporate REGEX or use ANTLR kind of parser, where it is easy to parse and build an object structure easily. I see that you have put in lot of time in making the library, but at the same time, its little difficult to modify it and use it properly, same is problem with other paid alternatives as well, their code also break and is difficult to trace. We have used ANTLR for parsing and without it, its just nightmare. – Akash Kava May 22 '12 at 11:37
-
2project is pretty dead, IDLE does not work properly http://mailsystem.codeplex.com/workitem/23586 – Greg Dean Nov 05 '12 at 05:41
-
Seems like all the things mentioned above have been implemented now and looks quite promising . And apparently SiteCore use it.. which is a big money making CMS so maybe theygave it new breath of life. Im gonna try this one – Piotr Kula Jun 18 '20 at 22:50
Try use the library : https://imapx.codeplex.com/
That library free, open source and have example at this : https://imapx.codeplex.com/wikipage?title=Sample%20code%20for%20get%20messages%20from%20your%20inbox

- 14,494
- 35
- 148
- 243

- 67
- 1
- 2
-
-
-
this library in my experience is significant slower than AE.Net.Mail - that makes no sense whatsoever but be warned! – Simon_Weaver Jul 04 '14 at 22:52
-
1
I haven't tried it myself, but this is a free library you could try (I not so sure about the SSL part on this one):
http://www.codeproject.com/KB/IP/imaplibrary.aspx
Also, there is xemail, which has parameters for SSL:
http://xemail-net.sourceforge.net/
[EDIT] If you (or the client) have the money for a professional mail-client, this thread has some good recommendations:
Recommendations for a .NET component to access an email inbox
-
the Imaplibrary at codeproject is what I was using but it does not have the needed functionality Ill checkout xemail – UnkwnTech Mar 21 '09 at 23:19
-
http://stackoverflow.com/questions/86553/working-with-pop-imap-email-and-net , ,I get this error: page not found – Kiquenet Nov 24 '10 at 14:49
-
-
REE: http://www.codeproject.com/KB/IP/imaplibrary.aspx in your answer; It's pretty cool. Have been using it since last week for an email client and haven't had any problems with it. – βӔḺṪẶⱫŌŔ Apr 12 '11 at 08:14