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

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Imap
oleobject loo_Messages
long i
oleobject loo_Verifier
oleobject loo_Message
long li_Uid
long li_Count
long li_Sig
oleobject loo_Info
long li_HaveInfo

li_Success = 0

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

loo_Imap = create oleobject
li_rc = loo_Imap.ConnectToNewObject("Chilkat.Imap")
if li_rc < 0 then
    destroy loo_Imap
    MessageBox("Error","Connecting to COM object failed")
    return
end if
loo_Imap.Ssl = 1
loo_Imap.Port = 993
loo_Imap.RequireSslCertVerify = 1
loo_Imap.PeekMode = 1
loo_Imap.AutoDownloadAttachments = 1
li_Success = loo_Imap.Connect("imap.example.com")
if li_Success <> 1 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    return
end if

li_Success = loo_Imap.Login("IMAP_USERNAME","IMAP_PASSWORD")
if li_Success <> 1 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    return
end if

li_Success = loo_Imap.SelectMailbox("INBOX")
if li_Success <> 1 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    return
end if

//  Select the first ten sequence positions, then fetch by stable UID.
loo_Messages = create oleobject
li_rc = loo_Messages.ConnectToNewObject("Chilkat.MessageSet")

li_Success = loo_Imap.QueryMbx("1:10",1,loo_Messages)
if li_Success <> 1 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    destroy loo_Messages
    return
end if

i = 0
loo_Verifier = create oleobject
li_rc = loo_Verifier.ConnectToNewObject("Chilkat.Dkim")

loo_Message = create oleobject
li_rc = loo_Message.ConnectToNewObject("Chilkat.BinData")

do while (i < loo_Messages.Count)
    li_Uid = loo_Messages.GetId(i)
    Write-Debug "Message UID " + string(li_Uid)
    li_Success = loo_Imap.FetchSingleBd(li_Uid,1,loo_Message)
    if li_Success <> 1 then
        Write-Debug loo_Imap.LastErrorText
    else
        li_Count = loo_Verifier.NumDkimSigs(loo_Message)
        if li_Count = 0 then
            Write-Debug "No DKIM signatures."
        end if

        li_Sig = 0
        do while (li_Sig < li_Count)
            li_Success = loo_Verifier.DkimVerify(li_Sig,loo_Message)
            //  Read diagnostics before the next verification replaces them.
            loo_Info = create oleobject
            li_rc = loo_Info.ConnectToNewObject("Chilkat.JsonObject")

            li_HaveInfo = loo_Info.Load(loo_Verifier.VerifyInfo)
            if li_Success = 1 then
                Write-Debug "Signature " + string(li_Sig) + " verified."
            else
                if (li_HaveInfo = 1) AND (loo_Info.StringOfEquals("bodyHashVerified","no",1) = 1) then
                    Write-Debug "Signature " + string(li_Sig) + ": the body hash differs."
                else
                    Write-Debug "Signature " + string(li_Sig) + " could not be verified."
                end if

                Write-Debug loo_Verifier.LastErrorText
            end if

            if (li_HaveInfo = 1) AND (loo_Info.JsonTypeOf("domain") = 1) then
                Write-Debug "Signing domain: " + loo_Info.StringOf("domain") + "  Selector: " + loo_Info.StringOf("selector")
            end if

            li_Sig = li_Sig + 1
        loop
    end if

    i = i + 1
loop
li_Success = loo_Imap.Disconnect()


destroy loo_Imap
destroy loo_Messages
destroy loo_Verifier
destroy loo_Message
destroy loo_Info