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

Create an encrypted S/MIME file without SMTP

See more Email Object Examples

Encrypt a saved draft for a recipient with an RSA encryption certificate. This example uses AES-256-GCM for the content and RSA-OAEP for the content-encryption key. Confirm these choices are supported by the receiving mail client. RenderToMimeBd produces the protected bytes without submitting mail.

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("message.eml")
if ($success -ne $true) {
    $($email.LastErrorText)
    exit
}

#  Use the recipient's public encryption certificate. Its private key stays with the recipient.
$cert = New-Object Chilkat.Cert
$success = $cert.LoadFromFile("recipient.cer")
if ($success -ne $true) {
    $($cert.LastErrorText)
    exit
}

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

$email.Pkcs7CryptAlg = "aes-gcm"
$email.Pkcs7KeyLength = 256
$email.OaepPadding = $true
$email.OaepHash = "sha256"
$email.OaepMgfHash = "sha256"
$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("encrypted.eml")
if ($success -ne $true) {
    $("Failed: rendered.WriteFile.")
    exit
}

$("Saved encrypted.eml.")