Sample code for 30+ languages & platforms
C# Requires Chilkat v11.0.0+

Sign and encrypt an email with S/MIME

See more Email Object Examples

Use the sender’s signing PFX and the recipient’s public RSA certificate with with-attachments.eml, which has one recipient. Supply your own signing-key password from a credential store. Render to bytes to apply both S/MIME operations without connecting to SMTP.

Chilkat C# Downloads

C#
bool success = false;

//  Assumes Chilkat is unlocked. See Global Unlock Sample.

Chilkat.Email email = new Chilkat.Email();
success = email.LoadEml("with-attachments.eml");
if (success != true) {
    Debug.WriteLine(email.LastErrorText);
    return;
}

Chilkat.Cert signer = new Chilkat.Cert();
success = signer.LoadPfxFile("signer.pfx","SIGNER_PASSWORD");
if (success != true) {
    Debug.WriteLine(signer.LastErrorText);
    return;
}

Chilkat.Cert recipient = new Chilkat.Cert();
success = recipient.LoadFromFile("recipient.cer");
if (success != true) {
    Debug.WriteLine(recipient.LastErrorText);
    return;
}

success = email.SetSigningCert(signer);
if (success != true) {
    Debug.WriteLine(email.LastErrorText);
    return;
}

success = email.SetEncryptCert(recipient);
if (success != true) {
    Debug.WriteLine(email.LastErrorText);
    return;
}

email.SigningHashAlg = "sha256";
email.Pkcs7CryptAlg = "aes";
email.Pkcs7KeyLength = 256;
email.OaepPadding = true;
email.OaepHash = "sha256";
email.OaepMgfHash = "sha256";
email.SendSigned = true;
email.SendEncrypted = true;
Chilkat.MailMan mail = new Chilkat.MailMan();
Chilkat.BinData rendered = new Chilkat.BinData();
success = mail.RenderToMimeBd(email,rendered);
if (success != true) {
    Debug.WriteLine(mail.LastErrorText);
    return;
}

success = rendered.WriteFile("signed-encrypted.eml");
if (success != true) {
    Debug.WriteLine("Failed: rendered.WriteFile.");
    return;
}

Debug.WriteLine("Saved signed-encrypted.eml.");