Tcl
Tcl
Send Encrypted Email using Certificate in Apple Keychain
See more Apple Keychain Examples
Send encrypted (S/MIME) email using a certificate found in the Apple Keychain.Note: This example requires Chilkat v10.1.2 or later.
Chilkat Tcl Downloads
load ./chilkat.dll
set success 0
set email [new_CkEmail]
CkEmail_put_Subject $email "This email is encrypted"
CkEmail_put_Body $email "This is a S/MIME encrypted mail"
CkEmail_put_From $email "Joe <joe@example.com>"
# Emails are encrypted using the recipient's certificate.
set recipientEmailAddr "jane@example2.com"
CkEmail_AddTo $email "" $recipientEmailAddr
# Indicate that the email is to be sent encrypted.
CkEmail_put_SendEncrypted $email 1
set cert [new_CkCert]
# -----------------------------------------------
# This example requires Chilkat v10.1.2 or later.
# -----------------------------------------------
# The recipient's certificate is used to encrypt.
# This will load the certificate from the Apple Keychain.
set success [CkCert_LoadByEmailAddress $cert $recipientEmailAddr]
if {$success != 1} then {
puts [CkCert_lastErrorText $cert]
delete_CkEmail $email
delete_CkCert $cert
exit
}
# Specify the certificate to be used for encryption.
set success [CkEmail_SetEncryptCert $email $cert]
CkEmail_put_SendEncrypted $email 1
set mailman [new_CkMailMan]
CkMailMan_put_SmtpHost $mailman "smtp.example.com"
CkMailMan_put_SmtpUsername $mailman "joe@example.com"
CkMailMan_put_SmtpPort $mailman 587
CkMailMan_put_StartTLS $mailman 1
# We'll get the SMTP password from the Apple Keychain.
# See how we originally saved the email credentials to the Keychain here:
# Save Email Credentials in Apple Keychain or Windows Credentials Manager
set secrets [new_CkSecrets]
# On Windows, this is the Windows Credentials Manager
# On MacOS/iOS, it is the Apple Keychain
CkSecrets_put_Location $secrets "local_manager"
# Specify the name of the secret.
# service and username are required.
# appName and domain are optional.
# Note: The values are arbitrary and can be anything you want.
set json [new_CkJsonObject]
CkJsonObject_UpdateString $json "appName" "MyEmailApp"
CkJsonObject_UpdateString $json "service" "SMTP"
CkJsonObject_UpdateString $json "domain" "example.com"
CkJsonObject_UpdateString $json "username" "joe@example.com"
CkMailMan_put_SmtpPassword $mailman [CkSecrets_getSecretStr $secrets $json]
# Send the encrypted email.
# The email is encrypted and sent within the SendEmail function.
set success [CkMailMan_SendEmail $mailman $email]
if {$success != 1} then {
puts [CkMailMan_lastErrorText $mailman]
delete_CkEmail $email
delete_CkCert $cert
delete_CkMailMan $mailman
delete_CkSecrets $secrets
delete_CkJsonObject $json
exit
}
puts "Encrypted email sent!"
delete_CkEmail $email
delete_CkCert $cert
delete_CkMailMan $mailman
delete_CkSecrets $secrets
delete_CkJsonObject $json