Sample code for 30+ languages & platforms
Tcl

Set the Decryption Certificate and Private Key

See more POP3 Examples

Demonstrates the Chilkat MailMan.SetDecryptCert2 method, which explicitly specifies both the certificate and its associated private key, as separate objects, to use when decrypting S/MIME encrypted email. This example loads a .cer certificate and a PEM private key, registers both, and fetches a message Chilkat can decrypt.

Background: Credentials are not always packaged together. When the certificate is a standalone .cer and the private key is a separate PEM (or other) file, SetDecryptCert2 takes the two objects individually. It is the two-object counterpart to SetDecryptCert, which expects a single certificate that already carries its private key (such as one loaded from a PFX).

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

#  Demonstrates the MailMan.SetDecryptCert2 method, which explicitly specifies both the
#  certificate and its associated private key (as separate objects) to use when decrypting
#  S/MIME encrypted email.

set mailman [new_CkMailMan]

#  Configure the POP3 server connection.
CkMailMan_put_MailHost $mailman "pop.example.com"
CkMailMan_put_MailPort $mailman 995
CkMailMan_put_PopSsl $mailman 1
CkMailMan_put_PopUsername $mailman "user@example.com"
CkMailMan_put_PopPassword $mailman "myPassword"

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

set success [CkCert_LoadFromFile $cert "qa_data/certs/recipient.cer"]
if {$success == 0} then {
    puts [CkCert_lastErrorText $cert]
    delete_CkMailMan $mailman
    delete_CkCert $cert
    exit
}

set privKey [new_CkPrivateKey]

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

#  Specify the certificate and private key to use when decrypting downloaded email.
set success [CkMailMan_SetDecryptCert2 $mailman $cert $privKey]
if {$success == 0} then {
    puts [CkMailMan_lastErrorText $mailman]
    delete_CkMailMan $mailman
    delete_CkCert $cert
    delete_CkPrivateKey $privKey
    exit
}

#  Fetch a message; if it is encrypted, Chilkat decrypts it using the certificate and key.
set email [new_CkEmail]

set success [CkMailMan_FetchOne $mailman 0 0 1 $email]
if {$success == 0} then {
    puts [CkMailMan_lastErrorText $mailman]
    delete_CkMailMan $mailman
    delete_CkCert $cert
    delete_CkPrivateKey $privKey
    delete_CkEmail $email
    exit
}

puts "Decrypted = [CkEmail_get_Decrypted $email]"

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

#  Note: Explicitly connecting/authenticating is optional.  Chilkat MailMan automatically
#  connects and authenticates -- using the property settings above -- whenever a server
#  operation requires it.  Calling the explicit connect/authenticate methods can still be
#  helpful to determine whether a failure occurs while connecting or while authenticating.

delete_CkMailMan $mailman
delete_CkCert $cert
delete_CkPrivateKey $privKey
delete_CkEmail $email