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

POP3 Verify Signed (S/MIME) Email

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

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oMailman
LOCAL oStUidls
LOCAL oEmail
LOCAL oCert
LOCAL nCount
LOCAL i

nSuccess := 0

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

oMailman := CreateObject("Chilkat.MailMan")

//  Set the POP3 server's hostname
oMailman:MailHost := "pop.example.com"

//  Set the POP3 login/password.
oMailman:PopUsername := "myLogin"
oMailman:PopPassword := "myPassword"

oStUidls := CreateObject("Chilkat.StringTable")
nSuccess := oMailman:FetchUidls(oStUidls)
IF (nSuccess == 0)
    ? oMailman:LastErrorText
    oMailman:destroy()
    oStUidls:destroy()
    RETURN
ENDIF

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

nCount := oStUidls:Count
i := 0
DO WHILE i < nCount
    //  Download the full email.
    nSuccess := oMailman:FetchByUidl(oStUidls:StringAt(i), 0, 0, oEmail)
    IF (nSuccess == 0)
        ? oMailman:LastErrorText
        oMailman:destroy()
        oStUidls:destroy()
        oEmail:destroy()
        oCert:destroy()
        RETURN
    ENDIF

    ? Str(i)
    ? "From: " + oEmail:From
    ? "Subject: " + oEmail:Subject

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

            nSuccess := oEmail:LastSignerCert(0, oCert)
            IF (nSuccess == 0)
                ? oEmail:LastErrorText
                oMailman:destroy()
                oStUidls:destroy()
                oEmail:destroy()
                oCert:destroy()
                RETURN
            ENDIF

            ? "Signing cert: " + oCert:SubjectCN
        ENDIF

    ELSE
        ? "Digital signature verification failed."
    ENDIF

    i := i + 1
ENDDO

oMailman:Pop3EndSession()

oMailman:destroy()
oStUidls:destroy()
oEmail:destroy()
oCert:destroy()