Sample code for 30+ languages & platforms
PureBasic

IMAP Download and Verify Signed (S/MIME) Email

See more IMAP Examples

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

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkCert.pb"
IncludeFile "CkImap.pb"
IncludeFile "CkMessageSet.pb"
IncludeFile "CkEmail.pb"

Procedure ChilkatExample()

    success.i = 0

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

    imap.i = CkImap::ckCreate()
    If imap.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ; Connect to an IMAP server.
    ; Use TLS
    CkImap::setCkSsl(imap, 1)
    CkImap::setCkPort(imap, 993)
    success = CkImap::ckConnect(imap,"imap.example.com")
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        ProcedureReturn
    EndIf

    success = CkImap::ckLogin(imap,"myLogin","myPassword")
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        ProcedureReturn
    EndIf

    ; Select an IMAP mailbox
    success = CkImap::ckSelectMailbox(imap,"Inbox")
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        ProcedureReturn
    EndIf

    ; We can choose to fetch UIDs or sequence numbers.
    fetchUids.i = 1

    ; Get the message IDs of all the emails in the mailbox
    messageSet.i = CkMessageSet::ckCreate()
    If messageSet.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkImap::ckQueryMbx(imap,"ALL",fetchUids,messageSet)
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        CkMessageSet::ckDispose(messageSet)
        ProcedureReturn
    EndIf

    email.i = CkEmail::ckCreate()
    If email.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    cert.i = CkCert::ckCreate()
    If cert.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    i.i = 0
    While i < CkMessageSet::ckCount(messageSet)

        uid.s = CkMessageSet::ckGetId(messageSet,i)
        Debug "uid: " + uid

        success = CkImap::ckFetchEmail(imap,0,uid,1,email)
        If success = 0
            Debug CkImap::ckLastErrorText(imap)
            CkImap::ckDispose(imap)
            CkMessageSet::ckDispose(messageSet)
            CkEmail::ckDispose(email)
            CkCert::ckDispose(cert)
            ProcedureReturn
        EndIf

        ; 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::ckReceivedSigned(email) = 1

            Debug "This email was signed."

            ; Check to see if the signatures were verified.
            If CkEmail::ckSignaturesValid(email) = 1
                Debug "Digital signature(s) verified."
                Debug "Signer: " + CkEmail::ckSignedBy(email)

                ; Get the certificate used for signing.
                success = CkEmail::ckLastSignerCert(email,0,cert)

                If success = 0
                    Debug "Failed to get signing certificate object."
                Else
                    Debug "Signing cert: " + CkCert::ckSubjectCN(cert)
                EndIf

            Else
                Debug "Digital signature verification failed."
            EndIf

        EndIf

        i = i + 1
    Wend

    ; Disconnect from the IMAP server.
    success = CkImap::ckDisconnect(imap)


    CkImap::ckDispose(imap)
    CkMessageSet::ckDispose(messageSet)
    CkEmail::ckDispose(email)
    CkCert::ckDispose(cert)


    ProcedureReturn
EndProcedure