Sample code for 30+ languages & platforms
PowerShell 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 PowerShell Downloads

PowerShell
Add-Type -Path "C:\chilkat\ChilkatDotNet47-x64\ChilkatDotNet47.dll"

$success = $false

#  Assumes Chilkat is unlocked. See Global Unlock Sample.

$email = New-Object Chilkat.Email
$success = $email.LoadEml("with-attachments.eml")
if ($success -ne $true) {
    $($email.LastErrorText)
    exit
}

$signer = New-Object Chilkat.Cert
$success = $signer.LoadPfxFile("signer.pfx","SIGNER_PASSWORD")
if ($success -ne $true) {
    $($signer.LastErrorText)
    exit
}

$recipient = New-Object Chilkat.Cert
$success = $recipient.LoadFromFile("recipient.cer")
if ($success -ne $true) {
    $($recipient.LastErrorText)
    exit
}

$success = $email.SetSigningCert($signer)
if ($success -ne $true) {
    $($email.LastErrorText)
    exit
}

$success = $email.SetEncryptCert($recipient)
if ($success -ne $true) {
    $($email.LastErrorText)
    exit
}

$email.SigningHashAlg = "sha256"
$email.Pkcs7CryptAlg = "aes"
$email.Pkcs7KeyLength = 256
$email.OaepPadding = $true
$email.OaepHash = "sha256"
$email.OaepMgfHash = "sha256"
$email.SendSigned = $true
$email.SendEncrypted = $true
$mail = New-Object Chilkat.MailMan
$rendered = New-Object Chilkat.BinData
$success = $mail.RenderToMimeBd($email,$rendered)
if ($success -ne $true) {
    $($mail.LastErrorText)
    exit
}

$success = $rendered.WriteFile("signed-encrypted.eml")
if ($success -ne $true) {
    $("Failed: rendered.WriteFile.")
    exit
}

$("Saved signed-encrypted.eml.")