Sample code for 30+ languages & platforms
Tcl

IMAP Read Encrypted Email

See more IMAP Examples

Demonstrates how to read encrypted email from an IMAP mailbox.

Reading encrypted email works the same as reading non-encrypted email. If the required certificate and private key are available on the system (e.g., in the macOS Keychain or Windows Certificate Store), Chilkat will automatically locate them, decrypt the email, and handle the process seamlessly.

Information about the original encrypted state of the email is available after it has been downloaded and decrypted.

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

set imap [new_CkImap]

CkImap_put_Ssl $imap 1
CkImap_put_Port $imap 993
set success [CkImap_Connect $imap "imap.example2.com"]
if {$success == 0} then {
    puts [CkImap_lastErrorText $imap]
    delete_CkImap $imap
    exit
}

# We'll get the IMAP email account's password from the Apple Keychain or Windows Credentials Manager.
# 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" "IMAP"
CkJsonObject_UpdateString $json "domain" "example2.com"
CkJsonObject_UpdateString $json "username" "jane@example2.com"

set password [CkSecrets_getSecretStr $secrets $json]
if {[CkSecrets_get_LastMethodSuccess $secrets] == 0} then {
    puts [CkSecrets_lastErrorText $secrets]
    delete_CkImap $imap
    delete_CkSecrets $secrets
    delete_CkJsonObject $json
    exit
}

set success [CkImap_Login $imap "jane@example2.com" $password]
if {$success == 0} then {
    puts [CkImap_lastErrorText $imap]
    delete_CkImap $imap
    delete_CkSecrets $secrets
    delete_CkJsonObject $json
    exit
}

# Select an IMAP mailbox
set success [CkImap_SelectMailbox $imap "Inbox"]
if {$success == 0} then {
    puts [CkImap_lastErrorText $imap]
    delete_CkImap $imap
    delete_CkSecrets $secrets
    delete_CkJsonObject $json
    exit
}

# This example: Send Encrypted Email using Certificate in Apple Keychain
# sent an email with the subject "This email is encrypted".
# Let's download an email with the word "encrypted" in the subject.

set messageSet [new_CkMessageSet]

set success [CkImap_QueryMbx $imap "SUBJECT encrypted" 1 $messageSet]
if {$success == 0} then {
    puts [CkImap_lastErrorText $imap]
    delete_CkImap $imap
    delete_CkSecrets $secrets
    delete_CkJsonObject $json
    delete_CkMessageSet $messageSet
    exit
}

if {[CkMessageSet_get_Count $messageSet] == 0} then {
    puts "No messages found."
    delete_CkImap $imap
    delete_CkSecrets $secrets
    delete_CkJsonObject $json
    delete_CkMessageSet $messageSet
    exit
}

# Reading encrypted email works the same as reading non-encrypted email. 
# If the required certificate and private key are available on the system (e.g., in the macOS Keychain or Windows Certificate Store), 
# Chilkat will automatically locate them, decrypt the email, and handle the process seamlessly.

set uid [CkMessageSet_GetId $messageSet 0]

set email [new_CkEmail]

set success [CkImap_FetchEmail $imap 0 $uid 1 $email]
if {$success == 0} then {
    puts [CkImap_lastErrorText $imap]
    delete_CkImap $imap
    delete_CkSecrets $secrets
    delete_CkJsonObject $json
    delete_CkMessageSet $messageSet
    delete_CkEmail $email
    exit
}

# Here we can show if the email was received encrypted, if it was successfully decrypted, and
# which certificate was used to decrypt.
puts "Email received encrypted: [CkEmail_get_ReceivedEncrypted $email]"

# Was it successfully decrypted?
puts "Successfully decrypted: [CkEmail_get_Decrypted $email]"

# What cert was used to decrypt?
puts "Encrypted by: [CkEmail_encryptedBy $email]"
set cert [new_CkCert]

set success [CkEmail_LastDecryptCert $email $cert]
if {$success != 0} then {
    puts "Certificate DN: [CkCert_subjectDN $cert]"
}

# Show the decrypted email body.
puts [CkEmail_body $email]

CkImap_Disconnect $imap

delete_CkImap $imap
delete_CkSecrets $secrets
delete_CkJsonObject $json
delete_CkMessageSet $messageSet
delete_CkEmail $email
delete_CkCert $cert