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

Unicode C++
#include <CkImapW.h>
#include <CkMessageSetW.h>
#include <CkDkimW.h>
#include <CkBinDataW.h>
#include <CkJsonObjectW.h>

void ChilkatSample(void)
    {
    bool success = false;

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

    CkImapW imap;
    imap.put_Ssl(true);
    imap.put_Port(993);
    imap.put_RequireSslCertVerify(true);
    imap.put_PeekMode(true);
    imap.put_AutoDownloadAttachments(true);
    success = imap.Connect(L"imap.example.com");
    if (success != true) {
        wprintf(L"%s\n",imap.lastErrorText());
        return;
    }

    success = imap.Login(L"IMAP_USERNAME",L"IMAP_PASSWORD");
    if (success != true) {
        wprintf(L"%s\n",imap.lastErrorText());
        return;
    }

    success = imap.SelectMailbox(L"INBOX");
    if (success != true) {
        wprintf(L"%s\n",imap.lastErrorText());
        return;
    }

    //  Select the first ten sequence positions, then fetch by stable UID.
    CkMessageSetW messages;
    success = imap.QueryMbx(L"1:10",true,messages);
    if (success != true) {
        wprintf(L"%s\n",imap.lastErrorText());
        return;
    }

    int i = 0;
    CkDkimW verifier;
    CkBinDataW message;
    while ((i < messages.get_Count())) {
        unsigned long uid = messages.GetId(i);
        wprintf(L"Message UID %u\n",uid);
        success = imap.FetchSingleBd(uid,true,message);
        if (success != true) {
            wprintf(L"%s\n",imap.lastErrorText());
        }
        else {
            int count = verifier.NumDkimSigs(message);
            if (count == 0) {
                wprintf(L"No DKIM signatures.\n");
            }

            int sig = 0;
            while ((sig < count)) {
                success = verifier.DkimVerify(sig,message);
                //  Read diagnostics before the next verification replaces them.
                CkJsonObjectW info;
                bool haveInfo = info.Load(verifier.verifyInfo());
                if (success == true) {
                    wprintf(L"Signature %d verified.\n",sig);
                }
                else {
                    if ((haveInfo == true) && (info.StringOfEquals(L"bodyHashVerified",L"no",true) == true)) {
                        wprintf(L"Signature %d: the body hash differs.\n",sig);
                    }
                    else {
                        wprintf(L"Signature %d could not be verified.\n",sig);
                    }

                    wprintf(L"%s\n",verifier.lastErrorText());
                }

                if ((haveInfo == true) && (info.JsonTypeOf(L"domain") == 1)) {
                    wprintf(L"Signing domain: %s  Selector: %s\n",info.stringOf(L"domain"),info.stringOf(L"selector"));
                }

                sig = sig + 1;
            }

        }

        i = i + 1;
    }

    success = imap.Disconnect();
    }