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

Go
    success := false

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

    imap := chilkat.NewImap()
    imap.SetSsl(true)
    imap.SetPort(993)
    imap.SetRequireSslCertVerify(true)
    imap.SetPeekMode(true)
    imap.SetAutoDownloadAttachments(true)
    success = imap.Connect("imap.example.com")
    if success != true {
        fmt.Println(imap.LastErrorText())
        imap.DisposeImap()
        return
    }

    success = imap.Login("IMAP_USERNAME","IMAP_PASSWORD")
    if success != true {
        fmt.Println(imap.LastErrorText())
        imap.DisposeImap()
        return
    }

    success = imap.SelectMailbox("INBOX")
    if success != true {
        fmt.Println(imap.LastErrorText())
        imap.DisposeImap()
        return
    }

    //  Select the first ten sequence positions, then fetch by stable UID.
    messages := chilkat.NewMessageSet()
    success = imap.QueryMbx("1:10",true,messages)
    if success != true {
        fmt.Println(imap.LastErrorText())
        imap.DisposeImap()
        messages.DisposeMessageSet()
        return
    }

    i := 0
    verifier := chilkat.NewDkim()
    message := chilkat.NewBinData()
    for (i < messages.Count()) {
        uid := messages.GetId(i)
        fmt.Println("Message UID ", uid)
        success = imap.FetchSingleBd(uid,true,message)
        if success != true {
            fmt.Println(imap.LastErrorText())
        } else {
            count := verifier.NumDkimSigs(message)
            if count == 0 {
                fmt.Println("No DKIM signatures.")
            }

            sig := 0
            for (sig < count) {
                success = verifier.DkimVerify(sig,message)
                //  Read diagnostics before the next verification replaces them.
                info := chilkat.NewJsonObject()
                haveInfo := info.Load(verifier.VerifyInfo())
                if success == true {
                    fmt.Println("Signature ", sig, " verified.")
                } else {
                    if (haveInfo == true) && (info.StringOfEquals("bodyHashVerified","no",true) == true) {
                        fmt.Println("Signature ", sig, ": the body hash differs.")
                    } else {
                        fmt.Println("Signature ", sig, " could not be verified.")
                    }

                    fmt.Println(verifier.LastErrorText())
                }

                if (haveInfo == true) && (info.JsonTypeOf("domain") == 1) {
                    fmt.Println("Signing domain: ", *info.StringOf("domain"), "  Selector: ", *info.StringOf("selector"))
                }

                sig = sig + 1
            }

        }

        i = i + 1
    }

    success = imap.Disconnect()

    imap.DisposeImap()
    messages.DisposeMessageSet()
    verifier.DisposeDkim()
    message.DisposeBinData()
    info.DisposeJsonObject()