Sample code for 30+ languages & platforms
Tcl

IMAP Download and Verify Signed (S/MIME) Email

See more IMAP Examples

Demonstrates how to download and verify digitally signed S/MIME email.

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

# This example requires the Chilkat API to have been previously unlocked.
# See Global Unlock Sample for sample code.

set imap [new_CkImap]

# Connect to an IMAP server.
# Use TLS
CkImap_put_Ssl $imap 1
CkImap_put_Port $imap 993
set success [CkImap_Connect $imap "imap.example.com"]
if {$success == 0} then {
    puts [CkImap_lastErrorText $imap]
    delete_CkImap $imap
    exit
}

set success [CkImap_Login $imap "myLogin" "myPassword"]
if {$success == 0} then {
    puts [CkImap_lastErrorText $imap]
    delete_CkImap $imap
    exit
}

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

# We can choose to fetch UIDs or sequence numbers.
set fetchUids 1

# Get the message IDs of all the emails in the mailbox
set messageSet [new_CkMessageSet]

set success [CkImap_QueryMbx $imap "ALL" $fetchUids $messageSet]
if {$success == 0} then {
    puts [CkImap_lastErrorText $imap]
    delete_CkImap $imap
    delete_CkMessageSet $messageSet
    exit
}

set email [new_CkEmail]

set cert [new_CkCert]

set i 0
while {$i < [CkMessageSet_get_Count $messageSet]} {

    set uid [CkMessageSet_GetId $messageSet $i]
    puts "uid: $uid"

    set success [CkImap_FetchEmail $imap 0 $uid 1 $email]
    if {$success == 0} then {
        puts [CkImap_lastErrorText $imap]
        delete_CkImap $imap
        delete_CkMessageSet $messageSet
        delete_CkEmail $email
        delete_CkCert $cert
        exit
    }

    # The security layers of signed and/or encrypted emails
    # are automatically "unwrapped" when loaded into
    # a Chilkat email object.
    # An application only needs to check to see if an email
    # was received signed or encrypted, and then examine
    # the success/failure.  For example:
    if {[CkEmail_get_ReceivedSigned $email] == 1} then {

        puts "This email was signed."

        # Check to see if the signatures were verified.
        if {[CkEmail_get_SignaturesValid $email] == 1} then {
            puts "Digital signature(s) verified."
            puts "Signer: [CkEmail_signedBy $email]"

            # Get the certificate used for signing.
            set success [CkEmail_LastSignerCert $email 0 $cert]

            if {$success == 0} then {
                puts "Failed to get signing certificate object."
            }             else {
                puts "Signing cert: [CkCert_subjectCN $cert]"
            }

        }         else {
            puts "Digital signature verification failed."
        }

    }

    set i [expr $i + 1]
}

# Disconnect from the IMAP server.
set success [CkImap_Disconnect $imap]

delete_CkImap $imap
delete_CkMessageSet $messageSet
delete_CkEmail $email
delete_CkCert $cert