+1 vote

How can i create and send SMIME in RFC 5322 format?

by
retagged by

1 Answer

0 votes
 
Best answer

Below is the code that creates SMIME signed and encrypted email message:

MailBuilder builder = new MailBuilder();
builder.Html = "<html><body>Encrypted and signed</body></html>";
builder.Subject = "Encrypted and signed";
builder.From.Add(new MailBox("email@in-the-certificate.com", "Alice"));
builder.To.Add(new MailBox("bob@mail.com", "Bob"));
builder.AddAttachment(@"c:\report_2014.pdf");

builder.SignWith(new X509Certificate2("SignCertificate.pfx", ""));
builder.EncryptWith(new X509Certificate2("EncryptCertificate.pfx", ""));
builder.EncryptWith(new X509Certificate2("BobsCertificate.pfx", ""));

IMail email = builder.Create();

Note that email is encrypted with both sender's and receiver's certificates. This way both parties are able to decrypt such email.

Here's the code that sends SMIME message using SMTP:

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

Here you can find all SMIME samples.

by (300k points)
selected by
...