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

Xojo Plugin
Dim success As Boolean
success = False

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

Dim imap As New Chilkat.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
    System.DebugLog(imap.LastErrorText)
    Return
End If

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

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

//  Select the first ten sequence positions, then fetch by stable UID.
Dim messages As New Chilkat.MessageSet
success = imap.QueryMbx("1:10",True,messages)
If (success <> True) Then
    System.DebugLog(imap.LastErrorText)
    Return
End If

Dim i As Int32
i = 0
Dim verifier As New Chilkat.Dkim
Dim message As New Chilkat.BinData
While (i < messages.Count)
    Dim uid As UInt32
    uid = messages.GetId(i)
    System.DebugLog("Message UID " + Str(uid))
    success = imap.FetchSingleBd(uid,True,message)
    If (success <> True) Then
        System.DebugLog(imap.LastErrorText)
    Else
        Dim count As Int32
        count = verifier.NumDkimSigs(message)
        If (count = 0) Then
            System.DebugLog("No DKIM signatures.")
        End If

        Dim sig As Int32
        sig = 0
        While (sig < count)
            success = verifier.DkimVerify(sig,message)
            //  Read diagnostics before the next verification replaces them.
            Dim info As New Chilkat.JsonObject
            Dim haveInfo As Boolean
            haveInfo = info.Load(verifier.VerifyInfo)
            If (success = True) Then
                System.DebugLog("Signature " + Str(sig) + " verified.")
            Else
                If ((haveInfo = True) And (info.StringOfEquals("bodyHashVerified","no",True) = True)) Then
                    System.DebugLog("Signature " + Str(sig) + ": the body hash differs.")
                Else
                    System.DebugLog("Signature " + Str(sig) + " could not be verified.")
                End If

                System.DebugLog(verifier.LastErrorText)
            End If

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

            sig = sig + 1
        Wend
    End If

    i = i + 1
Wend
success = imap.Disconnect()