{"id":1306,"date":"2010-10-25T15:50:41","date_gmt":"2010-10-25T13:50:41","guid":{"rendered":"http:\/\/www.limilabs.com\/blog\/index.php\/use-ssl-with-smtp"},"modified":"2023-09-22T16:00:48","modified_gmt":"2023-09-22T14:00:48","slug":"use-tls-ssl-with-smtp-net","status":"publish","type":"post","link":"https:\/\/www.limilabs.com\/blog\/use-tls-ssl-with-smtp-net","title":{"rendered":"Use TLS\/SSL with SMTP in .NET"},"content":{"rendered":"\n<p><a href=\"\/mail\">Mail.dll SMTP .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 sending.<\/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 SMTP client immediately connects using secure channel,<\/li>\n\n\n\n<li><strong>Explicit <\/strong>&#8211; where Mail.dll SMTP 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>Smtp.SSLConfiguration<\/em> property.<\/p>\n\n\n\n<p><em>Smtp<\/em> client may decide to secure the channel, if SMTP 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>.<\/div>\n\n\n\n<h2 class=\"wp-block-heading\">SMTP implicit TLS\/SSL mode<\/h2>\n\n\n\n<p>Mail.dll SMTP 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, SMTP over TLS\/SSL is associated with port 465, but this is not always the case. You can always specify different, then standard port, using <em>ConnectSSL<\/em> method overload.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n\/\/ C# version\n\nusing Limilabs.Mail;\nusing Limilabs.Client.POP3;\n\nclass Program\n{\n    static void Main(string&#x5B;] args)\n    {\n        using (Smtp smtp = new Smtp())\n        {\n            smtp.ConnectSSL(\"mail.example.com\");\n\n            smtp.UseBestLogin(\"user\", \"password\");\n\n            MailBuilder builder = new MailBuilder();\n            builder.Text = \"text\";\n            builder.From.Add(new MailBox(\"from@example.com\"));\n            builder.To.Add(new MailBox(\"to@example.com\"));\n\n            IMail email = builder.Create();\n\n            smtp.SendMessage(email);\n\n            smtp.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 Limilabs.Mail\nImports Limilabs.Client.SMTP\n\nPublic Module Module1\n    Public Sub Main(ByVal args As String())\n\n        Using smtp As New Smtp()\n\n            smtp.ConnectSSL(\"mail.example.com\")\n\n            smtp.UseBestLogin(\"user\", \"password\")\n\n            Dim builder As New MailBuilder()\n            builder.Text = \"text\"\n            builder.From.Add(New MailBox(\"from@example.com\"))\n            builder.&#x5B;To].Add(New MailBox(\"to@example.com\"))\n\n            Dim email As IMail = builder.Create()\n\n            smtp.SendMessage(email)\n\n            smtp.Close()\n        End Using\n\n    End Sub\nEnd Module\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">SMTP explicit TLS\/SSL mode<\/h2>\n\n\n\n<p>Mail.dll SMTP component connects using clear text channel and secures the channel using TLS\/SSL by issuing STARTTLS command. Typically standard SMTP ports: 25 or 587 are used, but this is not always the case. You can always specify different then standard port using <em>Connect<\/em> method overloads. By default 587 port is used.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n\/\/ C# version\n\nusing Limilabs.Mail;\nusing Limilabs.Mail.Headers;\nusing Limilabs.Client.SMTP;\n\nclass Program\n{\n    static void Main(string&#x5B;] args)\n    {\n        using (Smtp smtp = new Smtp())\n        {\n            smtp.Connect(\"mail.example.com\");\n\n            smtp.StartTLS();\n\n            smtp.UseBestLogin(\"user\", \"password\");\n\n            MailBuilder builder = new MailBuilder();\n            builder.Text = \"text\";\n            builder.From.Add(new MailBox(\"from@example.com\"));\n            builder.To.Add(new MailBox(\"to@example.com\"));\n\n            IMail email = builder.Create();\n\n            smtp.SendMessage(email);\n\n            smtp.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 Limilabs.Mail\nImports Limilabs.Mail.Headers\nImports Limilabs.Client.SMTP\n\nPublic Module Module1\n    Public Sub Main(ByVal args As String())\n\n        Using smtp As New Smtp()\n            smtp.Connect(\"mail.example.com\")\n\n            smtp.StartTLS()\n\n            smtp.UseBestLogin(\"user\", \"password\")\n\n            Dim builder As New MailBuilder()\n            builder.Text = \"text\"\n            builder.From.Add(New MailBox(\"from@example.com\"))\n            builder.&#x5B;To].Add(New MailBox(\"to@example.com\"))\n\n            Dim email As IMail = builder.Create()\n\n            smtp.SendMessage(email)\n\n            smtp.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 SMTP 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 = smtp.SupportedExtensions()\n   .Contains(SmtpExtension.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 = smtp.SupportedExtensions() _\n   .Contains(SmtpExtension.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 SMTP .NET email component supports Secure Socket Layer (SSL) and Transport Layer Security (TLS) protocols to authenticate the server and secure client-server email sending. 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,33,50,53,83,57],"class_list":["post-1306","post","type-post","status-publish","format-standard","hentry","category-mail-dll","tag-c","tag-email-component","tag-smtp","tag-ssl","tag-tls","tag-vb-net"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/1306"}],"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=1306"}],"version-history":[{"count":21,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/1306\/revisions"}],"predecessor-version":[{"id":6588,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/1306\/revisions\/6588"}],"wp:attachment":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/media?parent=1306"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/categories?post=1306"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/tags?post=1306"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}