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

Verify each DKIM signature in email downloaded over IMAP

See more DKIM Examples

Download the first ten messages (or all messages in a smaller inbox) as complete MIME, including attachments, while preserving their unread flags. The verifier looks up the selector’s TXT record when it needs a key. Inspect VerifyInfo immediately after each attempt for the signing domain, selector, and any body-hash mismatch.

Verify each signature independently, in top-to-bottom order. A passing signature authenticates the signing domain’s responsibility for the signed content. Identifying the author and checking DMARC alignment with the visible From address are separate steps. A missing DNS key or unavailable resolver can also prevent verification. DKIM specification.

Chilkat Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loImap
LOCAL loMessages
LOCAL i
LOCAL loVerifier
LOCAL loMessage
LOCAL lnUid
LOCAL lnCount
LOCAL lnSig
LOCAL loInfo
LOCAL lnHaveInfo

lnSuccess = 0

*  Assumes Chilkat is unlocked. See Global Unlock Sample.

loImap = CreateObject('Chilkat.Imap')
loImap.Ssl = 1
loImap.Port = 993
loImap.RequireSslCertVerify = 1
loImap.PeekMode = 1
loImap.AutoDownloadAttachments = 1
lnSuccess = loImap.Connect("imap.example.com")
IF (lnSuccess <> 1) THEN
    ? loImap.LastErrorText
    RELEASE loImap
    CANCEL
ENDIF

lnSuccess = loImap.Login("IMAP_USERNAME","IMAP_PASSWORD")
IF (lnSuccess <> 1) THEN
    ? loImap.LastErrorText
    RELEASE loImap
    CANCEL
ENDIF

lnSuccess = loImap.SelectMailbox("INBOX")
IF (lnSuccess <> 1) THEN
    ? loImap.LastErrorText
    RELEASE loImap
    CANCEL
ENDIF

*  Select the first ten sequence positions, then fetch by stable UID.
loMessages = CreateObject('Chilkat.MessageSet')
lnSuccess = loImap.QueryMbx("1:10",1,loMessages)
IF (lnSuccess <> 1) THEN
    ? loImap.LastErrorText
    RELEASE loImap
    RELEASE loMessages
    CANCEL
ENDIF

i = 0
loVerifier = CreateObject('Chilkat.Dkim')
loMessage = CreateObject('Chilkat.BinData')
DO WHILE (i < loMessages.Count)
    lnUid = loMessages.GetId(i)
    ? "Message UID " + STR(lnUid)
    lnSuccess = loImap.FetchSingleBd(lnUid,1,loMessage)
    IF (lnSuccess <> 1) THEN
        ? loImap.LastErrorText
    ELSE
        lnCount = loVerifier.NumDkimSigs(loMessage)
        IF (lnCount = 0) THEN
            ? "No DKIM signatures."
        ENDIF

        lnSig = 0
        DO WHILE (lnSig < lnCount)
            lnSuccess = loVerifier.DkimVerify(lnSig,loMessage)
            *  Read diagnostics before the next verification replaces them.
            loInfo = CreateObject('Chilkat.JsonObject')
            lnHaveInfo = loInfo.Load(loVerifier.VerifyInfo)
            IF (lnSuccess = 1) THEN
                ? "Signature " + STR(lnSig) + " verified."
            ELSE
                IF ((lnHaveInfo = 1) AND (loInfo.StringOfEquals("bodyHashVerified","no",1) = 1)) THEN
                    ? "Signature " + STR(lnSig) + ": the body hash differs."
                ELSE
                    ? "Signature " + STR(lnSig) + " could not be verified."
                ENDIF

                ? loVerifier.LastErrorText
            ENDIF

            IF ((lnHaveInfo = 1) AND (loInfo.JsonTypeOf("domain") = 1)) THEN
                ? "Signing domain: " + loInfo.StringOf("domain") + "  Selector: " + loInfo.StringOf("selector")
            ENDIF

            lnSig = lnSig + 1
        ENDDO
    ENDIF

    i = i + 1
ENDDO
lnSuccess = loImap.Disconnect()

RELEASE loImap
RELEASE loMessages
RELEASE loVerifier
RELEASE loMessage
RELEASE loInfo