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 <C_CkImapW.h>
#include <C_CkMessageSetW.h>
#include <C_CkDkimW.h>
#include <C_CkBinDataW.h>
#include <C_CkJsonObjectW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkImapW imap;
    HCkMessageSetW messages;
    int i;
    HCkDkimW verifier;
    HCkBinDataW message;
    unsigned long uid;
    int count;
    int sig;
    HCkJsonObjectW info;
    BOOL haveInfo;

    success = FALSE;

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

    imap = CkImapW_Create();
    CkImapW_putSsl(imap,TRUE);
    CkImapW_putPort(imap,993);
    CkImapW_putRequireSslCertVerify(imap,TRUE);
    CkImapW_putPeekMode(imap,TRUE);
    CkImapW_putAutoDownloadAttachments(imap,TRUE);
    success = CkImapW_Connect(imap,L"imap.example.com");
    if (success != TRUE) {
        wprintf(L"%s\n",CkImapW_lastErrorText(imap));
        CkImapW_Dispose(imap);
        return;
    }

    success = CkImapW_Login(imap,L"IMAP_USERNAME",L"IMAP_PASSWORD");
    if (success != TRUE) {
        wprintf(L"%s\n",CkImapW_lastErrorText(imap));
        CkImapW_Dispose(imap);
        return;
    }

    success = CkImapW_SelectMailbox(imap,L"INBOX");
    if (success != TRUE) {
        wprintf(L"%s\n",CkImapW_lastErrorText(imap));
        CkImapW_Dispose(imap);
        return;
    }

    //  Select the first ten sequence positions, then fetch by stable UID.
    messages = CkMessageSetW_Create();
    success = CkImapW_QueryMbx(imap,L"1:10",TRUE,messages);
    if (success != TRUE) {
        wprintf(L"%s\n",CkImapW_lastErrorText(imap));
        CkImapW_Dispose(imap);
        CkMessageSetW_Dispose(messages);
        return;
    }

    i = 0;
    verifier = CkDkimW_Create();
    message = CkBinDataW_Create();
    while ((i < CkMessageSetW_getCount(messages))) {
        uid = CkMessageSetW_GetId(messages,i);
        wprintf(L"Message UID %u\n",uid);
        success = CkImapW_FetchSingleBd(imap,uid,TRUE,message);
        if (success != TRUE) {
            wprintf(L"%s\n",CkImapW_lastErrorText(imap));
        }
        else {
            count = CkDkimW_NumDkimSigs(verifier,message);
            if (count == 0) {
                wprintf(L"No DKIM signatures.\n");
            }

            sig = 0;
            while ((sig < count)) {
                success = CkDkimW_DkimVerify(verifier,sig,message);
                //  Read diagnostics before the next verification replaces them.
                info = CkJsonObjectW_Create();
                haveInfo = CkJsonObjectW_Load(info,CkDkimW_verifyInfo(verifier));
                if (success == TRUE) {
                    wprintf(L"Signature %d verified.\n",sig);
                }
                else {
                    if ((haveInfo == TRUE) && (CkJsonObjectW_StringOfEquals(info,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",CkDkimW_lastErrorText(verifier));
                }

                if ((haveInfo == TRUE) && (CkJsonObjectW_JsonTypeOf(info,L"domain") == 1)) {
                    wprintf(L"Signing domain: %s  Selector: %s\n",CkJsonObjectW_stringOf(info,L"domain"),CkJsonObjectW_stringOf(info,L"selector"));
                }

                sig = sig + 1;
            }

        }

        i = i + 1;
    }

    success = CkImapW_Disconnect(imap);


    CkImapW_Dispose(imap);
    CkMessageSetW_Dispose(messages);
    CkDkimW_Dispose(verifier);
    CkBinDataW_Dispose(message);
    CkJsonObjectW_Dispose(info);

    }