{"id":1299,"date":"2010-10-25T15:46:49","date_gmt":"2010-10-25T13:46:49","guid":{"rendered":"http:\/\/www.limilabs.com\/blog\/?p=1299"},"modified":"2014-06-23T17:40:53","modified_gmt":"2014-06-23T15:40:53","slug":"send-signed-email-using-smime","status":"publish","type":"post","link":"https:\/\/www.limilabs.com\/blog\/send-signed-email-using-smime","title":{"rendered":"Send signed email using S\/MIME"},"content":{"rendered":"<p>In this article we&#8217;ll show how to digitally sign email message and send it using Mail.dll <a href=\"\/mail\">email component<\/a>. You&#8217;ll need to use S\/MIME (sometimes called SMIME) standard to sign email.<\/p>\n<p>S\/MIME (Secure\/Multipurpose Internet Mail Extensions) is a standard for public key encryption and signing of any MIME data including email messages.<\/p>\n<p>S\/MIME was originally developed by RSA Data Security. Specification uses Cryptographic Message Syntax (CMS), 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 <strong>digital signatures<\/strong>), privacy and data security (using encryption). <\/p>\n<p>S\/MIME signatures are usually done with what&#8217;s called &#8220;detached signatures&#8221;. The signature information is separate from the text being signed. The MIME type for such signed data is multipart\/signed with the second part having a MIME subtype of application\/(x-)pkcs7-signature. Mail.dll uses application\/x-pkcs7-signature MIME entity to store S\/MIME detached signatures.<\/p>\n<h2>Signing using MailBuilder<\/h2>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ C# version\r\n\r\nMailBuilder b = new MailBuilder();\r\nb.From.Add(new MailBox(&quot;mail@in_the_certificate.com&quot;, &quot;Alice&quot;));\r\nb.To.Add(new MailBox(&quot;bob@mail.com&quot;, &quot;Bob&quot;));\r\nb.Subject = &quot;Test&quot;;\r\nb.Html =                            \/\/ Set HTML body\r\n    &quot;This is &lt;strong&gt;signed&lt;\/strong&gt; message, &quot; +\r\n    &quot;with embedded image:&lt;br \/&gt;&quot; +\r\n    &quot;&lt;img src = 'cid:image1' \/&gt;.&quot;;\r\n\r\n\/\/ Read attachment from disk...and add it to Visuals collection\r\nMimeData image = b.AddVisual(@&quot;c:\\image.jpg&quot;);\r\nimage.ContentId = &quot;image1&quot;;\r\n\r\nb.SignWith(new X509Certificate2(&quot;TestCertificate.pfx&quot;, &quot;&quot;));\r\n\r\nIMail email = b.Create();\r\n<\/pre>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n' VB.NET\r\n\r\nDim b As New MailBuilder()\r\nb.From.Add(New MailBox(&quot;mail@in_the_certificate.com&quot;, &quot;Alice&quot;))\r\nb.&#x5B;To].Add(New MailBox(&quot;bob@mail.com&quot;, &quot;Bob&quot;))\r\nb.Subject = &quot;Test&quot;\r\n\r\n' Set HTML body\r\nb.Html = &quot;This is &lt;strong&gt;signed&lt;\/strong&gt; message, &quot; _\r\n   + &quot;with embedded image:&lt;br \/&gt;&quot; _\r\n   + &quot;&lt;img src = 'cid:image1' \/&gt;.&quot;\r\n\r\n' Read attachment from disk...and add it to Visuals collection\r\nDim image As MimeData = b.AddVisual(&quot;c:\\image.jpg&quot;)\r\nimage.ContentId = &quot;image1&quot;\r\n\r\nb.SignWith(New X509Certificate2(&quot;TestCertificate.pfx&quot;, &quot;&quot;))\r\n\r\nDim email As IMail = b.Create()\r\n<\/pre>\n<h2>Signing 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;This is &lt;strong&gt;signed&lt;\/strong&gt; message with image &lt;img src = 'cid:image1' \/&gt;&lt;\/body&gt;&lt;\/html&gt;&quot;)\r\n    .Subject(&quot;Test&quot;)\r\n    .From(new MailBox(&quot;mail@in_the_certificate.com&quot;, &quot;Alice&quot;))\r\n    .To(new MailBox(&quot;bob@mail.com&quot;, &quot;Bob&quot;))\r\n    .AddVisual(@&quot;c:\\image.jpg&quot;)\r\n    .SetContentId(&quot;image1&quot;)\r\n    .SignWith(new X509Certificate2(&quot;TestCertificate.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    .Html(&quot;&lt;html&gt;&lt;body&gt;This is &lt;strong&gt;signed&lt;\/strong&gt; message with image &lt;img src = 'cid:image1' \/&gt;&lt;\/body&gt;&lt;\/html&gt;&quot;) _\r\n    .Subject(&quot;Test&quot;) _\r\n    .From(New MailBox(&quot;mail@in_the_certificate.com&quot;, &quot;Alice&quot;)) _\r\n    .&#x5B;To](New MailBox(&quot;bob@mail.com&quot;, &quot;Bob&quot;)) _\r\n    .AddVisual(@&quot;c:\\image.jpg&quot;) _\r\n    .SetContentId(&quot;image1&quot;) _\r\n    .SignWith(New X509Certificate2(&quot;TestCertificate.pfx&quot;, &quot;&quot;)) _\r\n    .Create()\r\n<\/pre>\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 signed 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 SHA-1 alghoritm for signing. You can change this setting and <a href=\"\/blog\/send-signed-email-smime-sha-512\">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 digitally sign email message and send it using Mail.dll email component. You&#8217;ll need to use S\/MIME (sometimes called SMIME) standard to sign email. S\/MIME (Secure\/Multipurpose Internet Mail Extensions) is a standard for public key encryption and signing of any MIME data including email messages. S\/MIME was originally developed [&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-1299","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\/1299"}],"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=1299"}],"version-history":[{"count":14,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/1299\/revisions"}],"predecessor-version":[{"id":4696,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/1299\/revisions\/4696"}],"wp:attachment":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/media?parent=1299"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/categories?post=1299"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/tags?post=1299"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}