{"id":1218,"date":"2010-10-25T14:51:42","date_gmt":"2010-10-25T12:51:42","guid":{"rendered":"http:\/\/www.limilabs.com\/blog\/?p=1218"},"modified":"2024-07-29T19:20:26","modified_gmt":"2024-07-29T17:20:26","slug":"receive-unseen-emails-using-imap","status":"publish","type":"post","link":"https:\/\/www.limilabs.com\/blog\/receive-unseen-emails-using-imap","title":{"rendered":"Receive unseen emails using IMAP"},"content":{"rendered":"\n<p>This article describes how to receive unseen email messages using Mail.dll <a href=\"\/mail\">.net email library<\/a> and IMAP protocol.<\/p>\n\n\n\n<p>As a prerequisite you need to <strong>add reference to Mail.dll<\/strong> to your project. Please check MSDN <a href=\"https:\/\/docs.microsoft.com\/en-us\/visualstudio\/ide\/how-to-add-or-remove-references-by-using-the-reference-manager\" rel=\"nofollow\">how to add reference<\/a> article for details.<\/p>\n\n\n\n<p>When your reference is added you need to <strong>import correct namespaces<\/strong>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n\/\/ C#\n\nusing Limilabs.Mail;\nusing Limilabs.Client.IMAP;\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: vb; title: ; notranslate\" title=\"\">\n' VB\n\nImports Limilabs.Mail\nImports Limilabs.Client.IMAP\n<\/pre><\/div>\n\n\n<p>First thing you need to do is to <strong>connect to your IMAP server<\/strong>. <\/p>\n\n\n\n<p>Use <em>Connect(string host)<\/em> method to connect to the server. Typically IMAP server is working on port 143. You can use <em>Connect(string host, int port)<\/em> overload when you need to specify different port, or <em>ConnectSSL <\/em> methods to use <a href=\"\/blog\/use-tls-ssl-with-imap-net\" title=\"\">IMAP over TLS\/SSL<\/a>.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n\/\/ C#\n\nusing (Imap imap = new Imap ())\n{\n    imap.ConnectSSL(\"imap.example.com\");  \/\/ or Connect for no SSL\n    imap.UseBestLogin(\"user\", \"password\");\n\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: vb; title: ; notranslate\" title=\"\">\n' VB\n\nUsing imap As New Imap ()\n    imap.ConnectSSL(\"imap.example.com\")  ' or Connect for no SSL\n    imap.UseBestLogin(\"user\", \"password\")\n\n<\/pre><\/div>\n\n\n<p>Mail.dll supports <strong>OAuth 2.0<\/strong> for authentication, you can find <a href=\"\/mail\/samples#imap-office365-exchange\" title=\"\">OAuth2 samples for Office365 and Gmail on the Mail.dll samples page<\/a>. <\/p>\n\n\n\n<p>For <strong>Gmail <\/strong>you may want to use <a href=\"\/blog\/using-app-passwords-with-gmail\" title=\"\">Gmail&#8217;s App Passwords<\/a>.<\/p>\n\n\n\n<p>Next step is to select folder, <strong>download unique ids<\/strong> (uids) of unseen messages, and iterate through them. In this example we select Inbox, but you can use <em>Select<\/em> method to select different folder.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n\/\/ C#\n\nimap.SelectInbox();\n\nList&lt;long&gt; uids = imap.Search(Flag.Unseen);\nforeach (long uid in uids)\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: vb; title: ; notranslate\" title=\"\">\n' VB\n\nimap.SelectInbox()\n\nDim uids As List(Of Long) = imap.Search(Flag.Unseen)\nFor Each uid As Long In uids\n<\/pre><\/div>\n\n\n<p>Finally we&#8217;ll use <em>GetMessageByUID<\/em> method to download the message from the server and <em>MailBuilder <\/em>class to <strong>parse email and extract attachments<\/strong>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n\/\/ C#\n\nvar eml = imap.GetMessageByUID(uid)\nIMail email = new MailBuilder().CreateFromEml(eml);\n\nstring subject = email.Subject;\nstring plainText = email.Text;\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: vb; title: ; notranslate\" title=\"\">\n' VB\n\nDim eml = imap.GetMessageByUID(uid)\nDim email As IMail = builder.CreateFromEml(eml)\n\nConsole.WriteLine(email.Subject)\nConsole.WriteLine(email.Text)\n<\/pre><\/div>\n\n\n<p>At that point you can also <a href=\"\/blog\/save-all-attachments-to-disk-using-imap\">access and proceses email attachments<\/a>.<\/p>\n\n\n\n<p>Here are the full samples showing how to receive e-mail messages in C# and VB.NET:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n\/\/ C#\n\nusing Limilabs.Mail;\nusing Limilabs.Client.IMAP;\n\nclass Program\n{\n    static void Main(string&#x5B;] args)\n    {\n        using(Imap imap = new Imap())\n        {\n            imap.ConnectSSL(&quot;imap.example.com&quot;); \/\/ or Connect\n            imap.UseBestLogin(&quot;user&quot;, &quot;password&quot;);\n\n            imap.SelectInbox();\n            List&lt;long&gt; uids = imap.Search(Flag.Unseen);\n\n            foreach (long uid in uids)\n            {\n                var eml = imap.GetMessageByUID(uid);\n                IMail email = new MailBuilder()\n                    .CreateFromEml(eml);\n\n                string subject = email.Subject;\n                string plainText = email.Text;\n            }\n            imap.Close();\n        }\n    }\n};\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: vb; title: ; notranslate\" title=\"\">\n' VB.NET\n\nImports Limilabs.Mail\nImports Limilabs.Client.IMAP\n\nPublic Module Module1\n    Public Sub Main(ByVal args As String())\n        Using imap As New Imap()\n            imap.ConnectSSL(\"imap.example.com\") ' or Connect\n            imap.UseBestLogin(\"user\", \"password\")\n\n            imap.SelectInbox()\n            Dim uids As List(Of Long) = imap.Search(Flag.Unseen)\n\n            For Each uid As Long In uids\n                Dim eml = imap.GetMessageByUID(uid)\n                Dim email As IMail = New MailBuilder() _\n                    .CreateFromEml(eml)\n\n                Console.WriteLine(email.Subject)\n                Console.WriteLine(email.Text)\n            Next\n            imap.Close()\n        End Using\n    End Sub\nEnd Module\n<\/pre><\/div>\n\n\n<p>Just give <strong>Mail.dll a try <\/strong>and download it at: <a href=\"\/mail\">Mail.dll .NET email 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>This article describes how to receive unseen email messages using Mail.dll .net email library and IMAP protocol. As a prerequisite you need to add reference to Mail.dll to your project. Please check MSDN how to add reference article for details. When your reference is added you need to import correct namespaces: First thing you need [&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-1218","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\/1218"}],"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=1218"}],"version-history":[{"count":19,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/1218\/revisions"}],"predecessor-version":[{"id":6643,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/1218\/revisions\/6643"}],"wp:attachment":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/media?parent=1218"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/categories?post=1218"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/tags?post=1218"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}