{"id":1303,"date":"2010-10-25T15:49:37","date_gmt":"2010-10-25T13:49:37","guid":{"rendered":"http:\/\/www.limilabs.com\/blog\/?p=1303"},"modified":"2023-09-22T16:00:46","modified_gmt":"2023-09-22T14:00:46","slug":"use-tls-ssl-with-imap-net","status":"publish","type":"post","link":"https:\/\/www.limilabs.com\/blog\/use-tls-ssl-with-imap-net","title":{"rendered":"Use TLS\/SSL with IMAP in .NET"},"content":{"rendered":"\n<p><a href=\"\/mail\">Mail.dll IMAP .NET email component<\/a> supports Secure Socket Layer (<strong>SSL<\/strong>) and Transport Layer Security (<strong>TLS<\/strong>) protocols to authenticate the server and secure client-server email downloads.<\/p>\n\n\n\n<p>There are two modes in which Mail.dll can work:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Implicit <\/strong>&#8211; where Mail.dll IMAP client immediately connects using secure channel,<\/li>\n\n\n\n<li><strong>Explicit <\/strong>&#8211; where Mail.dll IMAP client connects on unsecured channel first and then secures the communication by issuing STARTTLS command. This mode is sometimes called TLS.<\/li>\n<\/ul>\n\n\n\n<p>In both cases, by default, Secure Sockets Layer (SSL) 3.0 and Transport Layer Security (TLS) 1.0, 1.1, 1.2, 1.3 are acceptable for secure communication. You can change the defaults using <em>Imap.SSLConfiguration<\/em> property.<\/p>\n\n\n\n<p><em>Imap<\/em> client may decide to secure the channel, if IMAP server explicitly forbids logging-in on unsecured channel and you are using <em>UseBestLogin<\/em> method.<\/p>\n\n\n\n<div class=\"well\">Here you can find more details on <a href=\"\/blog\/ssl-vs-tls-vs-starttls-stls\">SSL vs TLS vs STARTTLS<\/a>, and here how to <a href=\"\/blog\/use-tls12-with-imap\">turn on TLS 1.2 in IMAP<\/a>.<\/div>\n\n\n\n<h2 class=\"wp-block-heading\">IMAP implicit TLS\/SSL mode<\/h2>\n\n\n\n<p>Mail.dll IMAP component connects using secure TLS\/SSL channel. You need to know in advance, if the server supports TLS\/SSL connections &#8211; ask your administrator. Typically, IMAP over TLS\/SSL is associated with port 993, but this is not always the case. You can always specify different then standard port by using <em>ConnectSSL<\/em> method overloads.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n\/\/ C# version\n\nusing System;\nusing System.Collections.Generic;\nusing Limilabs.Client.IMAP;\nusing Limilabs.Mail;\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\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            }\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 version\n\nImports System\nImports System.Collections.Generic\nImports Limilabs.Client.IMAP\nImports Limilabs.Mail\n\nPublic Module Module1\n    Public Sub Main(ByVal args As String())\n\n        Using imap As New Imap()\n            imap.ConnectSSL(\"imap.example.com\")\n            imap.UseBestLogin(\"user\", \"password\")\n\n            imap.SelectInbox()\n\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                Dim subject As String = email.Subject\n            Next\n            imap.Close()\n        End Using\n\n    End Sub\nEnd Module\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">IMAP explicit TLS\/SSL mode<\/h2>\n\n\n\n<p>Mail.dll IMAP component connects using clear text channel and secures the channel using TLS\/SSL by issuing STARTTLS command. Typically standard IMAP port 143 is used, but this is not always the case. You can always specify different then standard port using <em>Connect<\/em> method overloads.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n\/\/ C# version\n\nusing System;\nusing System.Collections.Generic;\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.Connect(&quot;imap.example.com&quot;);\n            imap.StartTLS();\n\n            imap.UseBestLogin(&quot;user&quot;, &quot;password&quot;);\n\n            imap.SelectInbox();\n\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            }\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 version\n\nImports System\nImports System.Collections.Generic\nImports Limilabs.Mail\nImports Limilabs.Client.IMAP\n\nPublic Module Module1\n    Public Sub Main(ByVal args As String())\n\n        Using imap As New Imap()\n            imap.Connect(\"imap.example.com\")\n            imap.StartTLS()\n\n            imap.UseBestLogin(\"user\", \"password\")\n\n            imap.SelectInbox()\n\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                Dim subject As String = email.Subject\n            Next\n            imap.Close()\n        End Using\n\n    End Sub\nEnd Module\n<\/pre><\/div>\n\n\n<p>After you connect, you can check, if your IMAP server supports explicit TLS\/SSL using following code:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n\/\/ C# version\n\nbool supportsStartTLS = imap.SupportedExtensions()\n   .Contains(ImapExtension.StartTLS);\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: vb; title: ; notranslate\" title=\"\">\n' VB.NET version\n\nDim supportsStartTLS As Boolean = imap.SupportedExtensions() _\n   .Contains(ImapExtension.StartTLS)\n<\/pre><\/div>\n\n\n<p>You can read more here on how to know <a href=\"\/blog\/get-supported-server-extensions-imap-pop3-smtp\">which extensions does your server support<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Self-signed certificates<\/h2>\n\n\n\n<p>If you are using self-signed certificates you may encounter this error: <a href=\"\/blog\/the-remote-certificate-is-invalid-according-to-the-validation-procedure\">The remote certificate is invalid according to the validation procedure<\/a>.<\/p>\n\n\n\n<p><\/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 IMAP .NET email component supports Secure Socket Layer (SSL) and Transport Layer Security (TLS) protocols to authenticate the server and secure client-server email downloads. There are two modes in which Mail.dll can work: In both cases, by default, Secure Sockets Layer (SSL) 3.0 and Transport Layer Security (TLS) 1.0, 1.1, 1.2, 1.3 are acceptable [&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,53,83,57],"class_list":["post-1303","post","type-post","status-publish","format-standard","hentry","category-mail-dll","tag-c","tag-imap","tag-imap-component","tag-ssl","tag-tls","tag-vb-net"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/1303"}],"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=1303"}],"version-history":[{"count":26,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/1303\/revisions"}],"predecessor-version":[{"id":6587,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/1303\/revisions\/6587"}],"wp:attachment":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/media?parent=1303"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/categories?post=1303"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/tags?post=1303"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}