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

Tcl

load ./chilkat.dll

set success 0

#  Assumes Chilkat is unlocked. See Global Unlock Sample.

set email [new_CkEmail]

set success [CkEmail_LoadEml $email "message.eml"]
if {$success != 1} then {
    puts [CkEmail_lastErrorText $email]
    delete_CkEmail $email
    exit
}

#  Use the recipient's public encryption certificate. Its private key stays with the recipient.
set cert [new_CkCert]

set success [CkCert_LoadFromFile $cert "recipient.cer"]
if {$success != 1} then {
    puts [CkCert_lastErrorText $cert]
    delete_CkEmail $email
    delete_CkCert $cert
    exit
}

set success [CkEmail_SetEncryptCert $email $cert]
if {$success != 1} then {
    puts [CkEmail_lastErrorText $email]
    delete_CkEmail $email
    delete_CkCert $cert
    exit
}

CkEmail_put_Pkcs7CryptAlg $email "aes-gcm"
CkEmail_put_Pkcs7KeyLength $email 256
CkEmail_put_OaepPadding $email 1
CkEmail_put_OaepHash $email "sha256"
CkEmail_put_OaepMgfHash $email "sha256"
CkEmail_put_SendEncrypted $email 1
set mail [new_CkMailMan]

set rendered [new_CkBinData]

set success [CkMailMan_RenderToMimeBd $mail $email $rendered]
if {$success != 1} then {
    puts [CkMailMan_lastErrorText $mail]
    delete_CkEmail $email
    delete_CkCert $cert
    delete_CkMailMan $mail
    delete_CkBinData $rendered
    exit
}

set success [CkBinData_WriteFile $rendered "encrypted.eml"]
if {$success != 1} then {
    puts "Failed: rendered.WriteFile."
    delete_CkEmail $email
    delete_CkCert $cert
    delete_CkMailMan $mail
    delete_CkBinData $rendered
    exit
}

puts "Saved encrypted.eml."

delete_CkEmail $email
delete_CkCert $cert
delete_CkMailMan $mail
delete_CkBinData $rendered