Send signed email using S/MIME (SHA‑512)

In many cases it is required to use better that standard signing algorithms. With Mail.dll it is easy to specify algorithm that is used for signing. In this article we’ll show how to send S/MIME signed email using Mail.dll email component and specific signature algorithm (SHA-512).

If you don’t need to specify signature algorithm you can lean on default values: send signed email using S/MIME.

Signature algorithm selection is done through Algorithm property of the SignatureConfiguration class. You can use CommonOids class static properties to retrieve common oids (object identifiers): Sha1, Sha256, Sha512.

Signing using fluent interface

IMail mail = Mail.Text("text")
    .From("email@in-the-certificate.com")
    .SignWith(new SignatureConfiguration(new X509Certificate2(...)) 
        { 
            Algorithm = CommonOids.Sha512
        })
    .Create();

Signing using MailBuilder

MailBuilder builder = new MailBuilder();
builder.Text = "text";
builder.From.Add(new MailBox("email@in-the-certificate.com"));
builder.SignWith(new SignatureConfiguration(certificate) 
    { 
        Algorithm = CommonOids.Sha512
    });
IMail mail = builder.Create();

Sending email using SMTP

Now we can connect to SMTP server and send the email we recently created:

using (Smtp client = new Smtp())
{
    client.Connect("smtp.example.com"); // or ConnectSSL
    client.UseBestLogin("user", "password");
    client.SendMessage(email);
    client.Close();
}

Tags:     

Questions?

Consider using our Q&A forum for asking questions.