{"id":1693,"date":"2011-01-26T15:00:22","date_gmt":"2011-01-26T13:00:22","guid":{"rendered":"http:\/\/www.limilabs.com\/blog\/?p=1693"},"modified":"2014-07-15T15:51:33","modified_gmt":"2014-07-15T13:51:33","slug":"send-encrypted-email-using-smime","status":"publish","type":"post","link":"https:\/\/www.limilabs.com\/blog\/send-encrypted-email-using-smime","title":{"rendered":"Send encrypted email using S\/MIME"},"content":{"rendered":"<p>In this article we&#8217;ll show how to send digitally encrypted and signed emails (S\/MIME) using Mail.dll <a href=\"\/mail\">.NET email component<\/a>.<\/p>\n<p>S\/MIME (Secure\/Multipurpose Internet Mail Extensions) is a standard for public key encryption and signing of MIME data.<\/p>\n<p>S\/MIME was originally developed by RSA Data Security Inc. Specification uses Cryptographic Message Syntax, an IETF specification that is identical in most respects with PKCS #7. <\/p>\n<p>S\/MIME provides the following cryptographic security services for electronic messaging applications: authentication, message integrity, non-repudiation of origin (using digital signatures), privacy and data security (using <strong>encryption<\/strong>). S\/MIME specifies the MIME type application\/pkcs7-mime (smime-type &#8220;enveloped-data&#8221;) for data enveloping (encrypting) where the whole (prepared) MIME entity to be enveloped is encrypted and packed into an object which subsequently is inserted into an application\/pkcs7-mime MIME entity.<\/p>\n<h2>Encryption using MailBuilder<\/h2>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ C#\r\n\r\nMailBuilder builder = new MailBuilder();\r\nbuilder.Html = &quot;&lt;html&gt;&lt;body&gt;Encrypted and signed&lt;\/body&gt;&lt;\/html&gt;&quot;;\r\nbuilder.Subject = &quot;Encrypted and signed&quot;;\r\nbuilder.From.Add(new MailBox(&quot;email@in-the-certificate.com&quot;, &quot;Alice&quot;));\r\nbuilder.To.Add(new MailBox(&quot;bob@mail.com&quot;, &quot;Bob&quot;));\r\nbuilder.AddAttachment(@&quot;c:\\report_2014.pdf&quot;);\r\n\r\nbuilder.SignWith(new X509Certificate2(&quot;SignCertificate.pfx&quot;, &quot;&quot;));\r\nbuilder.EncryptWith(new X509Certificate2(&quot;EncryptCertificate.pfx&quot;, &quot;&quot;));\r\nbuilder.EncryptWith(new X509Certificate2(&quot;BobsCertificate.pfx&quot;, &quot;&quot;));\r\n\r\nIMail email = builder.Create();\r\n\r\n<\/pre>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n' VB.NET\r\n\r\nDim builder As New MailBuilder()\r\nbuilder.Html = &quot;&lt;html&gt;&lt;body&gt;Encrypted and signed&lt;\/body&gt;&lt;\/html&gt;&quot;\r\nbuilder.Subject = &quot;Encrypted and signed&quot;\r\nbuilder.From.Add(New MailBox(&quot;email@in-the-certificate.com&quot;, &quot;Alice&quot;))\r\nbuilder.&#x5B;To].Add(New MailBox(&quot;bob@mail.com&quot;, &quot;Bob&quot;))\r\nbuilder.AddAttachment(&quot;c:\\report_2014.pdf&quot;)\r\n\r\nbuilder.SignWith(New X509Certificate2(&quot;SignCertificate.pfx&quot;, &quot;&quot;))\r\nbuilder.EncryptWith(New X509Certificate2(&quot;EncryptCertificate.pfx&quot;, &quot;&quot;))\r\nbuilder.EncryptWith(New X509Certificate2(&quot;BobsCertificate.pfx&quot;, &quot;&quot;))\r\n\r\nDim email As IMail = builder.Create()\r\n<\/pre>\n<p>Remember to <strong>encrypt your emails with both sender&#8217;s and receiver&#8217;s certificates<\/strong>.<br \/>\nThis way both parties are able to decrypt such emails.<\/p>\n<h2>Encryption using fluent interface<\/h2>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ C# version\r\n\r\nIMail email = Mail\r\n    .Html(&quot;&lt;html&gt;&lt;body&gt;Encrypted and signed&lt;\/body&gt;&lt;\/html&gt;&quot;)\r\n    .Subject(&quot;Encrypted and signed&quot;)\r\n    .From(new MailBox(&quot;email@in-the-certificate.com&quot;, &quot;Alice&quot;))\r\n    .To(new MailBox(&quot;bob@mail.com&quot;, &quot;Bob&quot;))\r\n    .AddAttachment(@&quot;c:\\report_2014.pdf&quot;)\r\n    .SignWith(new X509Certificate2(&quot;SignCertificate.pfx&quot;, &quot;&quot;))\r\n    .EncryptWith(new X509Certificate2(&quot;EncryptCertificate.pfx&quot;, &quot;&quot;))\r\n    .EncryptWith(new X509Certificate2(&quot;BobsCertificate.pfx&quot;, &quot;&quot;))\r\n    .Create();\r\n<\/pre>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n' VB.NET\r\n\r\nDim email As IMail = Mail _\r\n\t.Html(&quot;&lt;html&gt;&lt;body&gt;Encrypted and signed&lt;\/body&gt;&lt;\/html&gt;&quot;) _\r\n\t.Subject(&quot;Encrypted and signed&quot;) _\r\n\t.From(New MailBox(&quot;email@in-the-certificate.com&quot;, &quot;Alice&quot;)) _\r\n\t.To(New MailBox(&quot;bob@mail.com&quot;, &quot;Bob&quot;)) _\r\n\t.AddAttachment(&quot;c:\\report_2014.pdf&quot;) _\r\n\t.SignWith(New X509Certificate2(&quot;SignCertificate.pfx&quot;, &quot;&quot;)) _\r\n\t.EncryptWith(New X509Certificate2(&quot;EncryptCertificate.pfx&quot;, &quot;&quot;)) _\r\n\t.EncryptWith(New X509Certificate2(&quot;BobsCertificate.pfx&quot;, &quot;&quot;)) _\r\n\t.Create()\r\n<\/pre>\n<h2>Common errors you may encounter<\/h2>\n<ul>\n<li>Please use the <strong>PersistKeySet <\/strong> flag when loading from file (new X509Certificate2(_certificatePath, &#8220;&#8221;, X509KeyStorageFlags.PersistKeySet);) and adding to store\n<\/li>\n<li><strong>&#8220;Bad key&#8221;<\/strong> exception message means that certificate was not for key exchange &#8211; makecert needs an extra parameter to create certificate that can be used for symmetric algorithm key exchange: -sky exchange.\n<\/li>\n<li><strong>&#8220;The enveloped-data message does not contain the specified recipient.&#8221;<\/strong> means that certificate with the private key is not deployed into the current account\/local machine personal store, or not in the certificates list\n<\/li>\n<\/ul>\n<h2>Create test certificate<\/h2>\n<p>You can use following commands in VisualStudio Command Prompt to create test certificate:<\/p>\n<p><code>makecert.exe -pe -r -sv Test_Keys.pvk -n \"CN=John Doe,E=email@in-the-certificate.com\" -sky exchange Test.cer<\/code><\/p>\n<p><code>pvk2pfx.exe -pvk Test_Keys.pvk -spc Test.cer -pfx Test.pfx<\/code><\/p>\n<div class=\"well\">\nIf you use CER or PEM files you can find more information in this article: <br \/>\n<a href=\"\/blog\/import-certificate-private-public-keys-pem-cer-pfx\">Importing private\/public keys or certificates in PEM, CER formats<\/a>.\n<\/div>\n<h2>Sending encrypted email using SMTP<\/h2>\n<p>Now we can connect to SMTP server and send the email we recently created:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ C#\r\n\r\nusing (Smtp client = new Smtp())\r\n{\r\n    client.Connect(&quot;smtp.example.com&quot;); \/\/ or ConnectSSL\r\n    client.UseBestLogin(&quot;user&quot;, &quot;password&quot;);\r\n    client.SendMessage(email);\r\n    client.Close();\r\n}\r\n<\/pre>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n' VB.NET\r\n\r\nUsing client As New Smtp()\r\n\tclient.Connect(&quot;smtp.example.com&quot;) ' or ConnectSSL\r\n\tclient.UseBestLogin(&quot;user&quot;, &quot;password&quot;)\r\n\tclient.SendMessage(email)\r\n\tclient.Close()\r\nEnd Using\r\n<\/pre>\n<p>By default Mail.dll uses TrippleDES (3-DES) for encryption and SHA-1 alghoritm for signing. You can change those settings and <a href=\"\/blog\/send-encrypted-email-smime-aes-256\">choose different signature and encryption algorithm while sending S\/MIME encrypted email message<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article we&#8217;ll show how to send digitally encrypted and signed emails (S\/MIME) using Mail.dll .NET email component. S\/MIME (Secure\/Multipurpose Internet Mail Extensions) is a standard for public key encryption and signing of MIME data. S\/MIME was originally developed by RSA Data Security Inc. Specification uses Cryptographic Message Syntax, an IETF specification that 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,33,49,50,57],"class_list":["post-1693","post","type-post","status-publish","format-standard","hentry","category-mail-dll","tag-c","tag-email-component","tag-smime","tag-smtp","tag-vb-net"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/1693"}],"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=1693"}],"version-history":[{"count":18,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/1693\/revisions"}],"predecessor-version":[{"id":4711,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/1693\/revisions\/4711"}],"wp:attachment":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/media?parent=1693"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/categories?post=1693"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/tags?post=1693"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}