Tcl
Tcl
IMAP Download and Verify Signed MIME
See more IMAP Examples
Downloads the original MIME of a digitally signed email and saves the .p7s signature along with other MIME parts. It then imports the email into a Chilkat email object to unwrap the S/MIME and verify the signature, and subsequently saves the attachments if they haven't been saved already.Chilkat Tcl Downloads
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
}
# Download the 1st email (as MIME) in the Inbox by sequence number.
set sbMime [new_CkStringBuilder]
set success [CkImap_FetchSingleAsMimeSb $imap 1 0 $sbMime]
if {$success == 0} then {
puts [CkImap_lastErrorText $imap]
delete_CkImap $imap
delete_CkStringBuilder $sbMime
exit
}
# Load it into a MIME object and check to see if it is signed
set mime [new_CkMime]
CkMime_LoadMimeSb $mime $sbMime
set alreadySavedParts 0
if {[CkMime_ContainsSignedParts $mime] == 1} then {
# This will save the .p7s and other parts...
set st [new_CkStringTable]
set success [CkMime_PartsToFiles $mime "c:/temp/qa_output" $st]
if {$success == 1} then {
set numFiles [CkStringTable_get_Count $st]
set i 0
while {$i < $numFiles} {
puts "Created: [CkStringTable_stringAt $st $i]"
set i [expr $i + 1]
}
set alreadySavedParts 1
}
}
# Load the MIME into an Email object. This unwraps the security layers and verifies signatures.
set email [new_CkEmail]
CkEmail_SetFromMimeSb $email $sbMime
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]"
# The certificate used for signing may be obtained
# by calling email.GetSignedByCert.
set cert [new_CkCert]
set success [CkEmail_LastSignerCert $email $i $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."
}
if {$alreadySavedParts != 1} then {
CkEmail_SaveAllAttachments $email "c:/temp/qa_output"
}
delete_CkImap $imap
delete_CkStringBuilder $sbMime
delete_CkMime $mime
delete_CkStringTable $st
delete_CkEmail $email
delete_CkCert $cert