Sample code for 30+ languages & platforms
Xbase++ Requires Chilkat v11.0.0+

IMAP Download and Verify Signed (S/MIME) Email

See more IMAP Examples

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

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oImap
LOCAL nFetchUids
LOCAL oMessageSet
LOCAL oEmail
LOCAL oCert
LOCAL i
LOCAL cUid

nSuccess := 0

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

oImap := CreateObject("Chilkat.Imap")

//  Connect to an IMAP server.
//  Use TLS
oImap:Ssl := 1
oImap:Port := 993
nSuccess := oImap:Connect("imap.example.com")
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    RETURN
ENDIF

nSuccess := oImap:Login("myLogin", "myPassword")
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    RETURN
ENDIF

//  Select an IMAP mailbox
nSuccess := oImap:SelectMailbox("Inbox")
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    RETURN
ENDIF

//  We can choose to fetch UIDs or sequence numbers.
nFetchUids := 1

//  Get the message IDs of all the emails in the mailbox
oMessageSet := CreateObject("Chilkat.MessageSet")
nSuccess := oImap:QueryMbx("ALL", nFetchUids, oMessageSet)
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    oMessageSet:destroy()
    RETURN
ENDIF

oEmail := CreateObject("Chilkat.Email")
oCert := CreateObject("Chilkat.Cert")

i := 0
DO WHILE i < oMessageSet:Count

    cUid := oMessageSet:GetId(i)
    ? "uid: " + cUid

    nSuccess := oImap:FetchEmail(0, cUid, 1, oEmail)
    IF (nSuccess == 0)
        ? oImap:LastErrorText
        oImap:destroy()
        oMessageSet:destroy()
        oEmail:destroy()
        oCert:destroy()
        RETURN
    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 (oEmail:ReceivedSigned == 1)

        ? "This email was signed."

        //  Check to see if the signatures were verified.
        IF (oEmail:SignaturesValid == 1)
            ? "Digital signature(s) verified."
            ? "Signer: " + oEmail:SignedBy

            //  Get the certificate used for signing.
            nSuccess := oEmail:LastSignerCert(0, oCert)

            IF (nSuccess == 0)
                ? "Failed to get signing certificate object."
            ELSE
                ? "Signing cert: " + oCert:SubjectCN
            ENDIF

        ELSE
            ? "Digital signature verification failed."
        ENDIF

    ENDIF

    i := i + 1
ENDDO

//  Disconnect from the IMAP server.
nSuccess := oImap:Disconnect()

oImap:destroy()
oMessageSet:destroy()
oEmail:destroy()
oCert:destroy()