Sample code for 30+ languages & platforms
Xbase++ 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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oImap
LOCAL oMessages
LOCAL i
LOCAL oVerifier
LOCAL oMessage
LOCAL nUid
LOCAL nCount
LOCAL nSig
LOCAL oInfo
LOCAL nHaveInfo

nSuccess := 0

//  Assumes Chilkat is unlocked. See Global Unlock Sample.

oImap := CreateObject("Chilkat.Imap")
oImap:Ssl := 1
oImap:Port := 993
oImap:RequireSslCertVerify := 1
oImap:PeekMode := 1
oImap:AutoDownloadAttachments := 1
nSuccess := oImap:Connect("imap.example.com")
IF (nSuccess != 1)
    ? oImap:LastErrorText
    oImap:destroy()
    RETURN
ENDIF

nSuccess := oImap:Login("IMAP_USERNAME", "IMAP_PASSWORD")
IF (nSuccess != 1)
    ? oImap:LastErrorText
    oImap:destroy()
    RETURN
ENDIF

nSuccess := oImap:SelectMailbox("INBOX")
IF (nSuccess != 1)
    ? oImap:LastErrorText
    oImap:destroy()
    RETURN
ENDIF

//  Select the first ten sequence positions, then fetch by stable UID.
oMessages := CreateObject("Chilkat.MessageSet")
nSuccess := oImap:QueryMbx("1:10", 1, oMessages)
IF (nSuccess != 1)
    ? oImap:LastErrorText
    oImap:destroy()
    oMessages:destroy()
    RETURN
ENDIF

i := 0
oVerifier := CreateObject("Chilkat.Dkim")
oMessage := CreateObject("Chilkat.BinData")
DO WHILE (i < oMessages:Count)
    nUid := oMessages:GetId(i)
    ? "Message UID " + Str(nUid)
    nSuccess := oImap:FetchSingleBd(nUid, 1, oMessage)
    IF (nSuccess != 1)
        ? oImap:LastErrorText
    ELSE
        nCount := oVerifier:NumDkimSigs(oMessage)
        IF (nCount == 0)
            ? "No DKIM signatures."
        ENDIF

        nSig := 0
        DO WHILE (nSig < nCount)
            nSuccess := oVerifier:DkimVerify(nSig, oMessage)
            //  Read diagnostics before the next verification replaces them.
            oInfo := CreateObject("Chilkat.JsonObject")
            nHaveInfo := oInfo:Load(oVerifier:VerifyInfo)
            IF (nSuccess == 1)
                ? "Signature " + Str(nSig) + " verified."
            ELSE
                IF ((nHaveInfo == 1) .AND. (oInfo:StringOfEquals("bodyHashVerified", "no", 1) == 1))
                    ? "Signature " + Str(nSig) + ": the body hash differs."
                ELSE
                    ? "Signature " + Str(nSig) + " could not be verified."
                ENDIF

                ? oVerifier:LastErrorText
            ENDIF

            IF ((nHaveInfo == 1) .AND. (oInfo:JsonTypeOf("domain") == 1))
                ? "Signing domain: " + oInfo:StringOf("domain") + "  Selector: " + oInfo:StringOf("selector")
            ENDIF

            nSig := nSig + 1
        ENDDO
    ENDIF

    i := i + 1
ENDDO
nSuccess := oImap:Disconnect()

oImap:destroy()
oMessages:destroy()
oVerifier:destroy()
oMessage:destroy()
oInfo:destroy()