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

Classic ASP
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<%
success = 0

'  Assumes Chilkat is unlocked. See Global Unlock Sample.

set imap = Server.CreateObject("Chilkat.Imap")
imap.Ssl = 1
imap.Port = 993
imap.RequireSslCertVerify = 1
imap.PeekMode = 1
imap.AutoDownloadAttachments = 1
success = imap.Connect("imap.example.com")
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( imap.LastErrorText) & "</pre>"
    Response.End
End If

success = imap.Login("IMAP_USERNAME","IMAP_PASSWORD")
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( imap.LastErrorText) & "</pre>"
    Response.End
End If

success = imap.SelectMailbox("INBOX")
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( imap.LastErrorText) & "</pre>"
    Response.End
End If

'  Select the first ten sequence positions, then fetch by stable UID.
set messages = Server.CreateObject("Chilkat.MessageSet")
success = imap.QueryMbx("1:10",1,messages)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( imap.LastErrorText) & "</pre>"
    Response.End
End If

i = 0
set verifier = Server.CreateObject("Chilkat.Dkim")
set message = Server.CreateObject("Chilkat.BinData")
Do While (i < messages.Count)
    uid = messages.GetId(i)
    Response.Write "<pre>" & Server.HTMLEncode( "Message UID " & uid) & "</pre>"
    success = imap.FetchSingleBd(uid,1,message)
    If (success <> 1) Then
        Response.Write "<pre>" & Server.HTMLEncode( imap.LastErrorText) & "</pre>"
    Else
        count = verifier.NumDkimSigs(message)
        If (count = 0) Then
            Response.Write "<pre>" & Server.HTMLEncode( "No DKIM signatures.") & "</pre>"
        End If

        sig = 0
        Do While (sig < count)
            success = verifier.DkimVerify(sig,message)
            '  Read diagnostics before the next verification replaces them.
            set info = Server.CreateObject("Chilkat.JsonObject")
            haveInfo = info.Load(verifier.VerifyInfo)
            If (success = 1) Then
                Response.Write "<pre>" & Server.HTMLEncode( "Signature " & sig & " verified.") & "</pre>"
            Else
                If ((haveInfo = 1) And (info.StringOfEquals("bodyHashVerified","no",1) = 1)) Then
                    Response.Write "<pre>" & Server.HTMLEncode( "Signature " & sig & ": the body hash differs.") & "</pre>"
                Else
                    Response.Write "<pre>" & Server.HTMLEncode( "Signature " & sig & " could not be verified.") & "</pre>"
                End If

                Response.Write "<pre>" & Server.HTMLEncode( verifier.LastErrorText) & "</pre>"
            End If

            If ((haveInfo = 1) And (info.JsonTypeOf("domain") = 1)) Then
                Response.Write "<pre>" & Server.HTMLEncode( "Signing domain: " & info.StringOf("domain") & "  Selector: " & info.StringOf("selector")) & "</pre>"
            End If

            sig = sig + 1
        Loop
    End If

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

%>
</body>
</html>