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

VB.NET
Dim success As Boolean = 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
    Debug.WriteLine(imap.LastErrorText)
    Exit Sub
End If

success = imap.Login("IMAP_USERNAME","IMAP_PASSWORD")
If (success <> True) Then
    Debug.WriteLine(imap.LastErrorText)
    Exit Sub
End If

success = imap.SelectMailbox("INBOX")
If (success <> True) Then
    Debug.WriteLine(imap.LastErrorText)
    Exit Sub
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
    Debug.WriteLine(imap.LastErrorText)
    Exit Sub
End If

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

        Dim sig As Integer = 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 = info.Load(verifier.VerifyInfo)
            If (success = True) Then
                Debug.WriteLine("Signature " & sig & " verified.")
            Else
                If ((haveInfo = True) And (info.StringOfEquals("bodyHashVerified","no",True) = True)) Then
                    Debug.WriteLine("Signature " & sig & ": the body hash differs.")
                Else
                    Debug.WriteLine("Signature " & sig & " could not be verified.")
                End If

                Debug.WriteLine(verifier.LastErrorText)
            End If

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

            sig = sig + 1
        End While
    End If

    i = i + 1
End While
success = imap.Disconnect()