Sending a digitally signed email from .NET Framework
Updated:
Note
The SmtpClient class used in this article is no longer recommended for new development. See the remarks section of the SmtpClient class reference for details. For new development, consider a library such as MailKit instead.
When you send email from .NET Framework, you would normally use SmtpClient. But SmtpClient has no property or simple option to digitally sign or encrypt a message. Because of that, this topic has been discussed on forums and blogs, and some third-party products are sold to fill the gap.
Note
When this article was originally written, it linked to a forum thread and a blog post about this topic. Both pages are no longer available, so this article no longer links to them.
Both the forum and the blog I found did not seem to consider Japanese text, so I wrote my own version that handles it. I show the full code at the end, but please use it at your own risk. It mostly reuses the existing SmtpClient, so it is not perfect. The code is patched together and has no exception handling.
First, handling Japanese text requires specifying a character encoding and encoding the string, so let’s create a class that Base64-encodes a string using the specified character encoding. Because a MIME header needs to indicate its character encoding, I provide two functions: a plain Encode, and EncodeWithCharecterCode, which also includes the character encoding name. You can use encodings such as iso-2022-jp or UTF-8.
Now, let’s build the message. Since this cannot be done with the standard SmtpClient features alone, we need to build the MIME data ourselves. First is the pattern with no attachments. The MIME data is built as follows.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
StringBuilder buffer = new StringBuilder(); string MimePart;
This is already getting tedious, but now let’s handle the case where there are attachments. This becomes a multi-part MIME message. The body part is mostly the same as before, so I won’t repeat it here. I’ve omitted the variable declarations, but the attachment data is built as follows, repeated once per attachment.
string Base64Value = Convert.ToBase64String(BinaryData, 0, BinaryData.Length); int Position = 0; while (Position < Base64Value.Length) { int ChunkSize = 100; if (Base64Value.Length - (Position + ChunkSize) < 0) ChunkSize = Base64Value.Length - Position; buffer.Append(Base64Value.Substring(Position, ChunkSize)); buffer.Append("\r\n"); Position += ChunkSize; } buffer.Append("\r\n");
Once the MIME data is ready, sign it. I’ll show how to select the certificate to use later.
1 2 3 4 5 6
byte[] Data = Encoding.ASCII.GetBytes(MimePart); ContentInfo Content = new ContentInfo(Data); SignedCms SignedCms = new SignedCms(Content, false); CmsSigner Signer = new CmsSigner(SubjectIdentifierType.IssuerAndSerialNumber, Certificate); SignedCms.ComputeSignature(Signer); byte[] SignedBytes = SignedCms.Encode();
After signing, set it on the MailMessage’s AlternateViews.
1 2 3
MemoryStream Stream = new MemoryStream(SignedBytes); AlternateView View = new AlternateView(Stream, "application/pkcs7-mime; smime-type=signed-data;name=smime.p7m"); Message.AlternateViews.Add(View);
Finally, send it with SmtpClient and you’re done.
1 2 3
SmtpClient Client = new SmtpClient("192.168.1.1", 25); Client.UseDefaultCredentials = true; Client.Send(Message);
For the signing certificate, you can either select it from the certificate store or load it from a file. Selecting it from the certificate store looks something like this.
1 2 3 4 5 6 7 8 9 10 11 12
X509Store Store = new X509Store(StoreLocation.CurrentUser); Store.Open(OpenFlags.OpenExistingOnly | OpenFlags.ReadOnly); X509Certificate2Collection Certs = Store.Certificates; foreach (X509Certificate2 Cert in Certs) { if (Cert.Subject.IndexOf("user01") >= 0) { Certificate = Cert; break; } }
If you load it from a certificate file that includes the private key, it looks like this.
1
Certificate = new X509Certificate2("C:\cert.pfx", "password");
That covers the individual pieces. Finally, here is the full sample code.
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Mail; using System.Security.Cryptography.Pkcs; using System.Security.Cryptography.X509Certificates; using System.Text; using System.Threading.Tasks;
As you’ll notice if you test this in various ways, this code is not perfect. For example, it doesn’t check the size of each line, and Outlook shows an attachment icon on the received message even when there are no attachments. To make it truly robust, you would need to implement the SMTP behavior yourself, on top of classes such as SmtpClient and MailMessage.