Sample code for 30+ languages & platforms
DataFlex

IMAP Download and Verify Signed (S/MIME) Email

See more IMAP Examples

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

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoImap
    Boolean iFetchUids
    Variant vMessageSet
    Handle hoMessageSet
    Variant vEmail
    Handle hoEmail
    Variant vCert
    Handle hoCert
    Integer i
    String sUid
    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

    // We can choose to fetch UIDs or sequence numbers.
    Move True To iFetchUids

    // Get the message IDs of all the emails in the mailbox
    Get Create (RefClass(cComChilkatMessageSet)) To hoMessageSet
    If (Not(IsComObjectCreated(hoMessageSet))) Begin
        Send CreateComObject of hoMessageSet
    End
    Get pvComObject of hoMessageSet to vMessageSet
    Get ComQueryMbx Of hoImap "ALL" iFetchUids vMessageSet To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatEmail)) To hoEmail
    If (Not(IsComObjectCreated(hoEmail))) Begin
        Send CreateComObject of hoEmail
    End
    Get Create (RefClass(cComChilkatCert)) To hoCert
    If (Not(IsComObjectCreated(hoCert))) Begin
        Send CreateComObject of hoCert
    End

    Move 0 To i
    While (i < (ComCount(hoMessageSet)))

        Get ComGetId Of hoMessageSet i To sUid
        Showln "uid: " sUid

        Get pvComObject of hoEmail to vEmail
        Get ComFetchEmail Of hoImap False sUid True vEmail To iSuccess
        If (iSuccess = False) Begin
            Get ComLastErrorText Of hoImap To sTemp1
            Showln sTemp1
            Procedure_Return
        End

        // 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:
        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

                // Get the certificate used for signing.
                Get pvComObject of hoCert to vCert
                Get ComLastSignerCert Of hoEmail 0 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
            Else Begin
                Showln "Digital signature verification failed."
            End

        End

        Move (i + 1) To i
    Loop

    // Disconnect from the IMAP server.
    Get ComDisconnect Of hoImap To iSuccess


End_Procedure