{"id":219,"date":"2023-11-29T13:51:36","date_gmt":"2023-11-29T11:51:36","guid":{"rendered":"http:\/\/www.limilabs.com\/blog\/?p=219"},"modified":"2025-07-03T15:35:56","modified_gmt":"2025-07-03T13:35:56","slug":"imap-component-for-net","status":"publish","type":"post","link":"https:\/\/www.limilabs.com\/blog\/imap-component-for-net","title":{"rendered":"IMAP component for .NET"},"content":{"rendered":"\n<p>Mail.dll email component includes an excellent <a href=\"\/mail\">IMAP email component<\/a>. Unleash the full potential of email management with Mail.dll&#8217;s outstanding IMAP email component.<\/p>\n\n\n\n<p>It offers .NET developers an unparalleled tool for handling IMAP email protocol. From retrieving messages to managing folders and attachments, the IMAP component in Mail.dll simplifies complex tasks into user-friendly functions.<\/p>\n\n\n\n<p>Mail.dll is available for all regular .NET framework versions as well as for most recent .net\u00a06, 7, 8 and<strong> .net\u00a09 <\/strong>and higher.<\/p>\n\n\n\n<p>IMAP, short for Internet Message Access Protocol, is one of the two most prevalent Internet standard protocols for e-mail retrieval, the other being Post Office Protocol &#8211; POP3 (also available in Mail.dll). <\/p>\n\n\n\n<p><a href=\"\/blog\/pop3-vs-imap\">IMAP has many advantages over POP3<\/a>, most important are using folders to group emails, saving seen\/unseen state of the messages and <a href=\"\/blog\/how-to-search-imap-in-net\">search capability<\/a>.<\/p>\n\n\n\n<p>This article is going to show how easy is to <strong>connect to use IMAP client<\/strong> from .NET, download and parse unseen messages.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Installation<\/h2>\n\n\n\n<p>The easiest way to install Mail.dll is to download it from nuget via Package Manager:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\nPM&gt; Install-Package Mail.dll\n<\/pre><\/div>\n\n\n<p>Alternatively you can <a href=\"\/mail\/download\">download Mail.dll directly<\/a> from our website.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Connect to IMAP<\/h2>\n\n\n\n<p>First you need connect to IMAP server. You should use <code>Imap<\/code> class for that. <code>Using<\/code> block ensures that IMAP connection is properly closed and disposed when you&#8217;re done.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nusing(Imap imap = new Imap())\n<\/pre><\/div>\n\n\n<p>In most cases you should be using TLS secured connection &#8211; <code>ConnectSSL<\/code> method will choose a correct port and perform SSL\/TLS negotiation, then you perform IMAP authentictation:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n\timap.ConnectSSL(\"imap.server.com\");\n\timap.UseBestLogin(\"user\", \"password\");\n<\/pre><\/div>\n\n\n<p>Mail.dll supports <strong>OAuth 2.0 <\/strong>for authentication, you can find <a href=\"\/mail\/samples\">OAuth2 samples for Office365 and Gmail on the Mail.dll samples<\/a> page.<\/p>\n\n\n\n<p>For <strong>Gmail<\/strong> you may want to use <a href=\"https:\/\/www.limilabs.com\/blog\/using-app-passwords-with-gmail\" title=\"Authenticate using Gmail\u2019s App Passwords to IMAP, POP3 and SMTP\">Gmail&#8217;s App Passwords<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Search for unseen emails<\/h2>\n\n\n\n<p>As IMAP groups messages into folder you need to select a well know INBOX folder. All new, received emails are first placed in the INBOX folder first:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nimap.SelectInbox();\n<\/pre><\/div>\n\n\n<p>Then you perform a search that find all unseen messages<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nList&lt;long&gt; uids = imap.Search(Flag.Unseen);\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Download and parse emails<\/h2>\n\n\n\n<p>Finally you download emails one-by-one and parse them. You should use <code>MailBuider<\/code> class to parse emails:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nvar eml = imap.GetMessageByUID(uid);\nIMail email = new MailBuilder().CreateFromEml(eml);\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Entire code to download emails in .net<\/h2>\n\n\n\n<p>Entire code to download emails using IMAP is less than 20 lines:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nusing(Imap imap = new Imap())\n{\n\timap.ConnectSSL(&quot;imap.server.com&quot;);\t\/\/ or Connect for no TLS\n\timap.UseBestLogin(&quot;user&quot;, &quot;password&quot;);\n\n\timap.SelectInbox();\n\tList&lt;long&gt; uids = imap.Search(Flag.Unseen);\n\n\tforeach (long uid in uids)\n\t{\n\t\tvar eml = imap.GetMessageByUID(uid);\n\t\tIMail email = new MailBuilder().CreateFromEml(eml);\n\n\t\tstring subject = mail.Subject;\n\t}\n\timap.Close();\n}\n<\/pre><\/div>\n\n\n<p>and for those who like VB.NET more:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: vb; title: ; notranslate\" title=\"\">\nUsing imap As New Imap()\n\timap.ConnectSSL(\"imap.server.com\")\n\timap.UseBestLogin(\"user\", \"password\")\n\n\timap.SelectInbox()\n\tDim uids As List(Of Long) = imap.Search(Flag.Unseen)\n\n\tFor Each uid As Long In uids\n\t\tDim mail As IMail = New MailBuilder()_\n\t\t\t.CreateFromEml(imap.GetMessageByUID(uid))\n\n\t\tConsole.WriteLine(mail.Subject)\n\tNext\n\timap.Close()\nEnd Using\n<\/pre><\/div>\n\n\n<p>It can&#8217;t get much simpler than that!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p>You can use Mail.dll .NET IMAP component to:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"\/blog\/folder-management-using-imap-create-delete-rename\">Create, delete and rename IMAP folders<\/a><\/li>\n\n\n\n<li>Download and <a href=\"\/blog\/uploading-emails-using-imap\">upload emails<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.limilabs.com\/blog\/download-email-attachments-net\" title=\"Download email attachments in .NET\">Download email attachments<\/a><\/li>\n\n\n\n<li><a href=\"\/blog\/move-emails-to-different-folder-with-imap\">Move and copy email messages between folders<\/a><\/li>\n\n\n\n<li>Flag messages<\/li>\n\n\n\n<li><a href=\"\/blog\/how-to-search-imap-in-net\">Powerful IMAP search<\/a> with easy to use syntax<\/li>\n\n\n\n<li>parse emails using fully tested (over 6000 unit tests) Mail.dll .NET email parser<\/li>\n<\/ul>\n\n\n\n<p>It works perfectly with Exchange, <strong>Office365<\/strong>, <strong>Gmail<\/strong> and all other IMAP servers.<\/p>\n\n\n\n<p>Just give <strong>Mail.dll a try <\/strong>and download it at: <a href=\"\/mail\">Mail.dll .NET IMAP component<\/a><\/p>\n\n\n\n<br \/>\n<a class=\"btn btn-primary btn-largest btn-action\" href=\"\/mail\/download\">Get Mail.dll<\/a>\n<br \/>\n","protected":false},"excerpt":{"rendered":"<p>Mail.dll email component includes an excellent IMAP email component. Unleash the full potential of email management with Mail.dll&#8217;s outstanding IMAP email component. It offers .NET developers an unparalleled tool for handling IMAP email protocol. From retrieving messages to managing folders and attachments, the IMAP component in Mail.dll simplifies complex tasks into user-friendly functions. Mail.dll is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[15,28,77,57],"class_list":["post-219","post","type-post","status-publish","format-standard","hentry","category-mail-dll","tag-c","tag-imap","tag-imap-component","tag-vb-net"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/219"}],"collection":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/comments?post=219"}],"version-history":[{"count":18,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/219\/revisions"}],"predecessor-version":[{"id":6673,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/219\/revisions\/6673"}],"wp:attachment":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/media?parent=219"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/categories?post=219"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/tags?post=219"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}