Sample code for 30+ languages & platforms
Tcl

Set the Signing Certificate and Private Key

See more Email Object Examples

Demonstrates the Chilkat Email.SetSigningCert2 method, which explicitly sets the certificate and its corresponding private key as separate objects for sending digitally signed email. This example loads a .cer certificate and a PEM private key, sets both, and enables signed sending.

Background: When your signing certificate and its private key are stored separately — a public .cer plus a PEM key file — rather than combined in a PFX, SetSigningCert2 accepts the two objects individually. It is the two-object counterpart to SetSigningCert, which takes a single certificate that already carries its private key.

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

#  Demonstrates the SetSigningCert2 method, which explicitly sets the certificate and its
#  corresponding private key (as separate objects) for sending digitally signed email.

set email [new_CkEmail]

CkEmail_put_Subject $email "Signed email"
CkEmail_put_Body $email "This message will be sent with a digital signature."
CkEmail_put_From $email "alice@example.com"
CkEmail_AddTo $email "Bob" "bob@example.com"

#  Load the certificate (public) and the matching private key from separate files.
set cert [new_CkCert]

set success [CkCert_LoadFromFile $cert "qa_data/certs/signer.cer"]
if {$success == 0} then {
    puts [CkCert_lastErrorText $cert]
    delete_CkEmail $email
    delete_CkCert $cert
    exit
}

set privKey [new_CkPrivateKey]

set success [CkPrivateKey_LoadPemFile $privKey "qa_data/certs/signer_privkey.pem"]
if {$success == 0} then {
    puts [CkPrivateKey_lastErrorText $privKey]
    delete_CkEmail $email
    delete_CkCert $cert
    delete_CkPrivateKey $privKey
    exit
}

#  Set the certificate and private key used to create the signature.
set success [CkEmail_SetSigningCert2 $email $cert $privKey]
if {$success == 0} then {
    puts [CkEmail_lastErrorText $email]
    delete_CkEmail $email
    delete_CkCert $cert
    delete_CkPrivateKey $privKey
    exit
}

CkEmail_put_SendSigned $email 1

puts "Signing certificate and private key set."

#  Note: Paths such as "qa_data/..." are relative local filesystem paths,
#  relative to the current working directory of the running application.

delete_CkEmail $email
delete_CkCert $cert
delete_CkPrivateKey $privKey