DataFlex
DataFlex
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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoImap
Variant vSbMime
Handle hoSbMime
Handle hoMime
Boolean iAlreadySavedParts
Variant vSt
Handle hoSt
Integer iNumFiles
Integer i
Handle hoEmail
Variant vCert
Handle hoCert
String sTemp1
Boolean bTemp1
Move False To iSuccess
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
Get Create (RefClass(cComChilkatImap)) To hoImap
If (Not(IsComObjectCreated(hoImap))) Begin
Send CreateComObject of hoImap
End
// Connect to an IMAP server.
// Use TLS
Set ComSsl Of hoImap To True
Set ComPort Of hoImap To 993
Get ComConnect Of hoImap "imap.example.com" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoImap To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComLogin Of hoImap "myLogin" "myPassword" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoImap To sTemp1
Showln sTemp1
Procedure_Return
End
// Select an IMAP mailbox
Get ComSelectMailbox Of hoImap "Inbox" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoImap To sTemp1
Showln sTemp1
Procedure_Return
End
// Download the 1st email (as MIME) in the Inbox by sequence number.
Get Create (RefClass(cComChilkatStringBuilder)) To hoSbMime
If (Not(IsComObjectCreated(hoSbMime))) Begin
Send CreateComObject of hoSbMime
End
Get pvComObject of hoSbMime to vSbMime
Get ComFetchSingleAsMimeSb Of hoImap 1 False vSbMime To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoImap To sTemp1
Showln sTemp1
Procedure_Return
End
// Load it into a MIME object and check to see if it is signed
Get Create (RefClass(cComChilkatMime)) To hoMime
If (Not(IsComObjectCreated(hoMime))) Begin
Send CreateComObject of hoMime
End
Get pvComObject of hoSbMime to vSbMime
Get ComLoadMimeSb Of hoMime vSbMime To iSuccess
Move False To iAlreadySavedParts
Get ComContainsSignedParts Of hoMime To bTemp1
If (bTemp1 = True) Begin
// This will save the .p7s and other parts...
Get Create (RefClass(cComChilkatStringTable)) To hoSt
If (Not(IsComObjectCreated(hoSt))) Begin
Send CreateComObject of hoSt
End
Get pvComObject of hoSt to vSt
Get ComPartsToFiles Of hoMime "c:/temp/qa_output" vSt To iSuccess
If (iSuccess = True) Begin
Get ComCount Of hoSt To iNumFiles
Move 0 To i
While (i < iNumFiles)
Get ComStringAt Of hoSt i To sTemp1
Showln "Created: " sTemp1
Move (i + 1) To i
Loop
Move True To iAlreadySavedParts
End
End
// Load the MIME into an Email object. This unwraps the security layers and verifies signatures.
Get Create (RefClass(cComChilkatEmail)) To hoEmail
If (Not(IsComObjectCreated(hoEmail))) Begin
Send CreateComObject of hoEmail
End
Get pvComObject of hoSbMime to vSbMime
Get ComSetFromMimeSb Of hoEmail vSbMime To iSuccess
Get ComReceivedSigned Of hoEmail To bTemp1
If (bTemp1 = True) Begin
Showln "This email was signed."
// Check to see if the signatures were verified.
Get ComSignaturesValid Of hoEmail To bTemp1
If (bTemp1 = True) Begin
Showln "Digital signature(s) verified."
Get ComSignedBy Of hoEmail To sTemp1
Showln "Signer: " sTemp1
// The certificate used for signing may be obtained
// by calling email.GetSignedByCert.
Get Create (RefClass(cComChilkatCert)) To hoCert
If (Not(IsComObjectCreated(hoCert))) Begin
Send CreateComObject of hoCert
End
Get pvComObject of hoCert to vCert
Get ComLastSignerCert Of hoEmail i vCert To iSuccess
If (iSuccess = False) Begin
Showln "Failed to get signing certificate object."
End
Else Begin
Get ComSubjectCN Of hoCert To sTemp1
Showln "Signing cert: " sTemp1
End
End
End
Else Begin
Showln "Digital signature verification failed."
End
If (iAlreadySavedParts <> True) Begin
Get ComSaveAllAttachments Of hoEmail "c:/temp/qa_output" To iSuccess
End
End_Procedure