{"id":5334,"date":"2018-12-08T22:50:05","date_gmt":"2018-12-08T20:50:05","guid":{"rendered":"https:\/\/www.limilabs.com\/blog\/?p=5334"},"modified":"2024-01-07T13:15:58","modified_gmt":"2024-01-07T11:15:58","slug":"receive-emails-in-net","status":"publish","type":"post","link":"https:\/\/www.limilabs.com\/blog\/receive-emails-in-net","title":{"rendered":"Receive emails in .NET"},"content":{"rendered":"\n<p>This article describes how to receive email messages using <a href=\"\/mail\">Mail.dll .NET email library<\/a>.<\/p>\n\n\n\n<p>Sending email is built into .NET, so there&#8217;s no need for 3rd party library, however Mail.dll is much easier to use than SmtpClient, and much more powerful. Receiving is a bit more complicated. It does require a 3rd party library.<\/p>\n\n\n\n<p>There are two standard protocols for receiving emails IMAP (Internet Message Access Protocol) and POP3 (Post Office Protocol). Without getting in to much details IMAP is better and offers more features when receiving emails (you can find a <a href=\"\/blog\/pop3-vs-imap\">detailed IMAP vs POP3 comparison here<\/a>). <\/p>\n\n\n\n<p>We&#8217;ll use <em>Imap <\/em>class to work with this protocol.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Installation<\/h2>\n\n\n\n<p>You can install Mail.dll client 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>or you can <a href=\"\/mail\/download\">download Mail.dll directly<\/a> from our website.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Namespaces<\/h2>\n\n\n\n<p>As a prerequisite you need to add reference to Mail.dll to your project and import following namespaces:<\/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 connect to your IMAP email server. Most servers today require SSL or TLS. We&#8217;ll use <em>ConnectSSL(string host)<\/em> to connect and establish secure channel. This method makes sure correct SSL\/TLS versions are used and server certificate is valid:<\/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\");\n    imap.UseBestLogin(\"user@example.com\", \"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\")\n    imap.UseBestLogin(\"user@example.com\", \"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\">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<p>Next step is to select a folder which we want to access and download unique ids (UIDs) of email messages. In this example we&#8217;ll search and receive <strong>unseen <\/strong>emails from <strong>INBOX <\/strong>folder.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n\/\/ C#\n\nimap.SelectInbox();\nList&lt;long&gt; uids = imap.Search(Flag.Unseen);\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: vb; title: ; notranslate\" title=\"\">\n' VB\n\nimap.SelectInbox()\nDim uids As List(Of Long) = imap.Search(Flag.Unseen)\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Receive emails<\/h2>\n\n\n\n<p>Finally we need to receive and process those emails. On the server emails are stored in MIME format. <em>GetMessageByUID<\/em> method receives emails as a raw byte array and <em>MailBuilder <\/em>class can be used to parse it:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n\/\/ C#\n\nforeach (long uid in uids)\n{\n    var eml = imap.GetMessageByUID(uid)\n    IMail email = new MailBuilder().CreateFromEml(eml);\n\n    Console.WriteLine(email.Subject);\n    Console.WriteLine(email.Text);\n}\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: vb; title: ; notranslate\" title=\"\">\n' VB\n\nFor Each uid As Long In uids\n{\n    Dim eml = imap.GetMessageByUID(uid)\n    Dim email As IMail = builder.CreateFromEml(eml)\n\n    Console.WriteLine(email.Subject)\n    Console.WriteLine(email.Text)\n}\n<\/pre><\/div>\n\n\n<p>At that point you can also <a href=\"\/blog\/save-all-attachments-to-disk-using-imap\">access attachments<\/a> or <a href=\"\/blog\/how-to-access-to-cc-bcc-fields\">From and To fields<\/a>.<\/p>\n\n\n\n<p>Here are the full samples that receive emails in both 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;);\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                Console.WriteLine(email.Subject);\n                Console.WriteLine(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\")\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=\"https:\/\/www.limilabs.com\/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>This article describes how to receive email messages using Mail.dll .NET email library. Sending email is built into .NET, so there&#8217;s no need for 3rd party library, however Mail.dll is much easier to use than SmtpClient, and much more powerful. Receiving is a bit more complicated. It does require a 3rd party library. There are [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[28,112],"class_list":["post-5334","post","type-post","status-publish","format-standard","hentry","category-mail-dll","tag-imap","tag-receive"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/5334"}],"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=5334"}],"version-history":[{"count":19,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/5334\/revisions"}],"predecessor-version":[{"id":6606,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/5334\/revisions\/6606"}],"wp:attachment":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/media?parent=5334"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/categories?post=5334"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/tags?post=5334"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}