Sample code for 30+ languages & platforms
AutoIt

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 AutoIt Downloads

AutoIt
Local $bSuccess = False

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

$oImap = ObjCreate("Chilkat.Imap")

; Connect to an IMAP server.
; Use TLS
$oImap.Ssl = True
$oImap.Port = 993
$bSuccess = $oImap.Connect("imap.example.com")
If ($bSuccess = False) Then
    ConsoleWrite($oImap.LastErrorText & @CRLF)
    Exit
EndIf

$bSuccess = $oImap.Login("myLogin","myPassword")
If ($bSuccess = False) Then
    ConsoleWrite($oImap.LastErrorText & @CRLF)
    Exit
EndIf

; Select an IMAP mailbox
$bSuccess = $oImap.SelectMailbox("Inbox")
If ($bSuccess = False) Then
    ConsoleWrite($oImap.LastErrorText & @CRLF)
    Exit
EndIf

; Download the 1st email (as MIME) in the Inbox by sequence number.
$oSbMime = ObjCreate("Chilkat.StringBuilder")
$bSuccess = $oImap.FetchSingleAsMimeSb(1,False,$oSbMime)
If ($bSuccess = False) Then
    ConsoleWrite($oImap.LastErrorText & @CRLF)
    Exit
EndIf

; Load it into a MIME object and check to see if it is signed
$oMime = ObjCreate("Chilkat.Mime")
$oMime.LoadMimeSb($oSbMime)
Local $bAlreadySavedParts = False
If ($oMime.ContainsSignedParts() = True) Then

    ; This will save the .p7s and other parts...
    $oSt = ObjCreate("Chilkat.StringTable")
    $bSuccess = $oMime.PartsToFiles("c:/temp/qa_output",$oSt)
    If ($bSuccess = True) Then
Local $iNumFiles = $oSt.Count
Local $i = 0
        While $i < $iNumFiles
            ConsoleWrite("Created: " & $oSt.StringAt($i) & @CRLF)
            $i = $i + 1
        Wend
        $bAlreadySavedParts = True
    EndIf

EndIf

; Load the MIME into an Email object.  This unwraps the security layers and verifies signatures.
$oEmail = ObjCreate("Chilkat.Email")
$oEmail.SetFromMimeSb($oSbMime)

If ($oEmail.ReceivedSigned = True) Then

    ConsoleWrite("This email was signed." & @CRLF)

    ; Check to see if the signatures were verified.
    If ($oEmail.SignaturesValid = True) Then
        ConsoleWrite("Digital signature(s) verified." & @CRLF)

        ConsoleWrite("Signer: " & $oEmail.SignedBy & @CRLF)

        ; The certificate used for signing may be obtained
        ; by calling email.GetSignedByCert.
        $oCert = ObjCreate("Chilkat.Cert")
        $bSuccess = $oEmail.LastSignerCert($i,$oCert)
        If ($bSuccess = False) Then
            ConsoleWrite("Failed to get signing certificate object." & @CRLF)
        Else
            ConsoleWrite("Signing cert: " & $oCert.SubjectCN & @CRLF)
        EndIf

    EndIf

Else
    ConsoleWrite("Digital signature verification failed." & @CRLF)
EndIf

If ($bAlreadySavedParts <> True) Then
    $oEmail.SaveAllAttachments("c:/temp/qa_output")
EndIf