Sample code for 30+ languages & platforms
Visual FoxPro

IMAP Download and Verify Signed (S/MIME) Email

See more IMAP Examples

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

Chilkat Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loImap
LOCAL lnFetchUids
LOCAL loMessageSet
LOCAL loEmail
LOCAL loCert
LOCAL i
LOCAL lcUid

lnSuccess = 0

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

loImap = CreateObject('Chilkat.Imap')

* Connect to an IMAP server.
* Use TLS
loImap.Ssl = 1
loImap.Port = 993
lnSuccess = loImap.Connect("imap.example.com")
IF (lnSuccess = 0) THEN
    ? loImap.LastErrorText
    RELEASE loImap
    CANCEL
ENDIF

lnSuccess = loImap.Login("myLogin","myPassword")
IF (lnSuccess = 0) THEN
    ? loImap.LastErrorText
    RELEASE loImap
    CANCEL
ENDIF

* Select an IMAP mailbox
lnSuccess = loImap.SelectMailbox("Inbox")
IF (lnSuccess = 0) THEN
    ? loImap.LastErrorText
    RELEASE loImap
    CANCEL
ENDIF

* We can choose to fetch UIDs or sequence numbers.
lnFetchUids = 1

* Get the message IDs of all the emails in the mailbox
loMessageSet = CreateObject('Chilkat.MessageSet')
lnSuccess = loImap.QueryMbx("ALL",lnFetchUids,loMessageSet)
IF (lnSuccess = 0) THEN
    ? loImap.LastErrorText
    RELEASE loImap
    RELEASE loMessageSet
    CANCEL
ENDIF

loEmail = CreateObject('Chilkat.Email')
loCert = CreateObject('Chilkat.Cert')

i = 0
DO WHILE i < loMessageSet.Count

    lcUid = loMessageSet.GetId(i)
    ? "uid: " + lcUid

    lnSuccess = loImap.FetchEmail(0,lcUid,1,loEmail)
    IF (lnSuccess = 0) THEN
        ? loImap.LastErrorText
        RELEASE loImap
        RELEASE loMessageSet
        RELEASE loEmail
        RELEASE loCert
        CANCEL
    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 (loEmail.ReceivedSigned = 1) THEN

        ? "This email was signed."

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

            * Get the certificate used for signing.
            lnSuccess = loEmail.LastSignerCert(0,loCert)

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

        ELSE
            ? "Digital signature verification failed."
        ENDIF

    ENDIF

    i = i + 1
ENDDO

* Disconnect from the IMAP server.
lnSuccess = loImap.Disconnect()

RELEASE loImap
RELEASE loMessageSet
RELEASE loEmail
RELEASE loCert