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

B4X
Dim success As Boolean = False

'  Assumes Chilkat is unlocked. See Global Unlock Sample.

Dim imap As ChilkatImap
imap.Initialize("imap")
imap.Ssl = True
imap.Port = 993
imap.RequireSslCertVerify = True
imap.PeekMode = True
imap.AutoDownloadAttachments = True
success = imap.Connect("imap.example.com")
If success <> True Then
    Log(imap.LastErrorText)
    Return
End If

success = imap.Login("IMAP_USERNAME", "IMAP_PASSWORD")
If success <> True Then
    Log(imap.LastErrorText)
    Return
End If

success = imap.SelectMailbox("INBOX")
If success <> True Then
    Log(imap.LastErrorText)
    Return
End If

'  Select the first ten sequence positions, then fetch by stable UID.
Dim messages As ChilkatMessageSet
messages.Initialize
success = imap.QueryMbx("1:10", True, messages)
If success <> True Then
    Log(imap.LastErrorText)
    Return
End If

Dim i As Int = 0
Dim verifier As ChilkatDkim
verifier.Initialize("verifier")
Dim message As ChilkatBinData
message.Initialize
Do While (i < messages.Count)
    Dim uid As Long = messages.GetId(i)
    Log("Message UID " & uid)
    success = imap.FetchSingleBd(uid, True, message)
    If success <> True Then
        Log(imap.LastErrorText)
    Else
        Dim count As Int = verifier.NumDkimSigs(message)
        If count = 0 Then
            Log("No DKIM signatures.")
        End If

        Dim sig As Int = 0
        Do While (sig < count)
            success = verifier.DkimVerify(sig, message)
            '  Read diagnostics before the next verification replaces them.
            Dim info As ChilkatJsonObject
            info.Initialize
            Dim haveInfo As Boolean = info.Load(verifier.VerifyInfo)
            If success = True Then
                Log("Signature " & sig & " verified.")
            Else
                If (haveInfo = True) And (info.StringOfEquals("bodyHashVerified", "no", True) = True) Then
                    Log("Signature " & sig & ": the body hash differs.")
                Else
                    Log("Signature " & sig & " could not be verified.")
                End If

                Log(verifier.LastErrorText)
            End If

            If (haveInfo = True) And (info.JsonTypeOf("domain") = 1) Then
                Log("Signing domain: " & info.StringOf("domain") & "  Selector: " & info.StringOf("selector"))
            End If

            sig = sig + 1
        Loop
    End If

    i = i + 1
Loop
success = imap.Disconnect