{"id":1801,"date":"2011-05-24T10:00:10","date_gmt":"2011-05-24T08:00:10","guid":{"rendered":"http:\/\/www.limilabs.com\/blog\/?p=1801"},"modified":"2016-09-09T11:00:29","modified_gmt":"2016-09-09T09:00:29","slug":"send-signed-email-receive-encrypted","status":"publish","type":"post","link":"https:\/\/www.limilabs.com\/blog\/send-signed-email-receive-encrypted","title":{"rendered":"Send signed email receive encrypted"},"content":{"rendered":"<p>In this article we&#8217;ll show how to create test certificates or use existing certificate, <strong>for sending signed emails<\/strong>. Our recipients will use the public key information from the signed email <strong>to encrypt emails<\/strong> they&#8217;ll be sending to us. Finally we&#8217;ll show how to <strong>decrypt <\/strong>those emails.<\/p>\n<h2>Create test certificate<\/h2>\n<p>We&#8217;ll use <em>makecert.exe<\/em> tool to create certificate in <em>cer <\/em>format and <em>pvk2pfx.exe<\/em> tool to convert it to <em>pfx <\/em>format:<\/p>\n<p><code><br \/>\nmakecert.exe -pe -r -sv Test_Keys.pvk -n \"CN=Alice,E=alice2@testdomain.com\" -sky exchange Test.cer<br \/>\n<\/code><br \/>\n<code><br \/>\npvk2pfx.exe -pvk Test_Keys.pvk -spc Test.cer -pfx Test.pfx<br \/>\n<\/code><\/p>\n<p><img decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/2011\/05\/1_CreateNewCertificate.png\" alt=\"\" title=\"1_CreateNewCertificate\" \/><\/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>Create S\/MIME signed email<\/h2>\n<p>Now we&#8217;ll <strong>create a signed message<\/strong> using Mail.dll. It is a simple task we just need to load certifcate from disk and use <em>SignWith<\/em> method:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nX509Certificate2 certificate = new X509Certificate2(\r\n    @&quot;c:\\Test.pfx&quot;;, \r\n    &quot;&quot;, \r\n    X509KeyStorageFlags.PersistKeySet);\r\n\r\nIMail email = Limilabs.Mail.Fluent.Mail.Text(&quot;This is a signed message&quot;)\r\n    .Subject(&quot;This is a signed message&quot;)\r\n    .From(&quot;alice2@testdomain.com&quot;)\r\n    .To(&quot;test@testdomain.com&quot;)\r\n    .SignWith(certificate)\r\n    .Create();\r\n<\/pre>\n<h2>Send S\/MIME signed email<\/h2>\n<p>Now we&#8217;ll use <em>Smtp <\/em>class to connect and authenticate to our SMTP server and send the email message:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nusing(Smtp smtp = new Smtp())\r\n{\r\n    smtp.Connect(&quot;smtp.server.com&quot;);  \/\/ or ConnectSSL for SSL\r\n    smtp.UseBestLogin(&quot;user&quot;, &quot;password&quot;);\r\n \r\n    smtp.SendMessage(email);                     \r\n \r\n    smtp.Close();   \r\n}              \r\n<\/pre>\n<p>Here you can find more details on <a href =\"\/blog\/send-signed-email-using-smime\">sending S\/MIME signed email<\/a>.<\/p>\n<h2>S\/MIME signed email is received<\/h2>\n<p>Here&#8217;s how the recipient will see the message. Please note that we are using<strong> self-signed certificates<\/strong> and this is why we are seeing this warning message.<\/p>\n<p><a href=\"\/blog\/wp-content\/uploads\/2011\/05\/3_ReceiveSignedEmail.png\"><img loading=\"lazy\" decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/2011\/05\/3_ReceiveSignedEmail-300x207.png\" alt=\"\" title=\"3_ReceiveSignedEmail\" width=\"300\" height=\"207\" class=\"alignnone size-medium\" \/><\/a><\/p>\n<p>Next step for the recipient is to <strong>mark received certificate as trusted<\/strong>.<\/p>\n<p><img decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/2011\/05\/4_Trust.png\" \/><\/p>\n<p>The recipient should then <strong>add the certificate to the contact<\/strong> list:<\/p>\n<p><a href=\"\/blog\/wp-content\/uploads\/2011\/05\/5_AddToContacts.png\"><img loading=\"lazy\" decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/2011\/05\/5_AddToContacts-300x191.png\" alt=\"\" title=\"5_AddToContacts\" width=\"300\" height=\"191\" class=\"alignnone size-medium\" \/><\/a><\/p>\n<p>As you can see there is a <strong>DigitalID <\/strong>assigned to Alice (email sender):<\/p>\n<p><img decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/2011\/05\/6_DigitalIDIsAdded.png\" alt=\"\" title=\"6_DigitalIDIsAdded\" \/><\/a><\/p>\n<h2>S\/MIME encrypted email reply<\/h2>\n<p>Finally recipient replies to the message marking the new message to be <strong>encrypted<\/strong>.<\/p>\n<p><img decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/2011\/05\/7_SendEncryptedEmail.png\" \/><\/p>\n<h2> Receiving S\/MIME encrypted email reply<\/h2>\n<p>We&#8217;ll use IMAP component to <a href=\"\/blog\/receive-unseen-emails-using-imap\">download this message<\/a>. You can use IMAP or <a href=\"\/blog\/receive-emails-using-pop3\">POP3 components to download it<\/a>.<\/p>\n<p>In fact we can see that it is encrypted (we are showing raw eml variable here):<\/p>\n<p><img decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/2011\/05\/8_ReceivedEmailIsEncrypted.png\" alt=\"\" title=\"8_ReceivedEmailIsEncrypted\" \/><\/p>\n<p>Now we can decrypt the message using the same certificate, we used for signing. Note that we are adding this certificate to <em>SMIMEConfiguration.Certificates<\/em> collection:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nX509Certificate2 certificate = new X509Certificate2(\r\n    @&quot;c:\\Text.pfx&quot;, \r\n    &quot;&quot;, \r\n    X509KeyStorageFlags.PersistKeySet);\r\n\r\nusing(Imap imap = new Imap())\r\n{\r\n    imap.Connect(&quot;imap.testdomain.com&quot;);\r\n    imap.UseBestLogin(&quot;alice2@testdomain.com&quot;, &quot;password&quot;);\r\n\r\n    var eml = imap.GetMessageByNumber(1);\r\n\r\n    MailBuilder builder = new MailBuilder();\r\n    builder.SMIMEConfiguration.Certificates.Add(certificate);\r\n    IMail email = builder.CreateFromEml(eml);\r\n\r\n    Console.WriteLine(email.IsEncrypted);\r\n    Console.WriteLine(email.Html);\r\n    Console.WriteLine(email.Text);\r\n\r\n    imap.Close();\r\n}\r\n<\/pre>\n<p><img decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/2011\/05\/9_CanDecrypt.png\" alt=\"\" title=\"9_CanDecrypt\" \/><\/p>\n<p>You can also find more information about SMIME and Mail.dll here:<\/p>\n<ul>\n<li><a href=\"\/blog\/send-signed-email-using-smime\">Send signed email using S\/MIME<\/a><\/li>\n<li><a href=\"\/blog\/send-encrypted-email-using-smime\">Send encrypted email using S\/MIME<\/a><\/li>\n<li><a href=\"\/blog\/validate-smime-emails\">Validate S\/MIME emails<\/a><\/li>\n<li><a href=\"\/blog\/decrypt-smime-emails\">Decrypt S\/MIME emails<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In this article we&#8217;ll show how to create test certificates or use existing certificate, for sending signed emails. Our recipients will use the public key information from the signed email to encrypt emails they&#8217;ll be sending to us. Finally we&#8217;ll show how to decrypt those emails. Create test certificate We&#8217;ll use makecert.exe tool to create [&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":[33,49],"class_list":["post-1801","post","type-post","status-publish","format-standard","hentry","category-mail-dll","tag-email-component","tag-smime"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/1801"}],"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=1801"}],"version-history":[{"count":10,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/1801\/revisions"}],"predecessor-version":[{"id":5092,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/1801\/revisions\/5092"}],"wp:attachment":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/media?parent=1801"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/categories?post=1801"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/tags?post=1801"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}