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

Delphi DLL
var
success: Boolean;
imap: HCkImap;
messages: HCkMessageSet;
i: Integer;
verifier: HCkDkim;
message: HCkBinData;
uid: Cardinal;
count: Integer;
sig: Integer;
info: HCkJsonObject;
haveInfo: Boolean;

begin
success := False;

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

imap := CkImap_Create();
CkImap_putSsl(imap,True);
CkImap_putPort(imap,993);
CkImap_putRequireSslCertVerify(imap,True);
CkImap_putPeekMode(imap,True);
CkImap_putAutoDownloadAttachments(imap,True);
success := CkImap_Connect(imap,'imap.example.com');
if (success <> True) then
  begin
    Memo1.Lines.Add(CkImap__lastErrorText(imap));
    Exit;
  end;
success := CkImap_Login(imap,'IMAP_USERNAME','IMAP_PASSWORD');
if (success <> True) then
  begin
    Memo1.Lines.Add(CkImap__lastErrorText(imap));
    Exit;
  end;
success := CkImap_SelectMailbox(imap,'INBOX');
if (success <> True) then
  begin
    Memo1.Lines.Add(CkImap__lastErrorText(imap));
    Exit;
  end;
//  Select the first ten sequence positions, then fetch by stable UID.
messages := CkMessageSet_Create();
success := CkImap_QueryMbx(imap,'1:10',True,messages);
if (success <> True) then
  begin
    Memo1.Lines.Add(CkImap__lastErrorText(imap));
    Exit;
  end;
i := 0;
verifier := CkDkim_Create();
message := CkBinData_Create();
while (i < CkMessageSet_getCount(messages)) do
  begin
    uid := CkMessageSet_GetId(messages,i);
    Memo1.Lines.Add('Message UID ' + IntToStr(uid));
    success := CkImap_FetchSingleBd(imap,uid,True,message);
    if (success <> True) then
      begin
        Memo1.Lines.Add(CkImap__lastErrorText(imap));
      end
    else
      begin
        count := CkDkim_NumDkimSigs(verifier,message);
        if (count = 0) then
          begin
            Memo1.Lines.Add('No DKIM signatures.');
          end;
        sig := 0;
        while (sig < count) do
          begin
            success := CkDkim_DkimVerify(verifier,sig,message);
            //  Read diagnostics before the next verification replaces them.
            info := CkJsonObject_Create();
            haveInfo := CkJsonObject_Load(info,CkDkim__verifyInfo(verifier));
            if (success = True) then
              begin
                Memo1.Lines.Add('Signature ' + IntToStr(sig) + ' verified.');
              end
            else
              begin
                if ((haveInfo = True) and (CkJsonObject_StringOfEquals(info,'bodyHashVerified','no',True) = True)) then
                  begin
                    Memo1.Lines.Add('Signature ' + IntToStr(sig) + ': the body hash differs.');
                  end
                else
                  begin
                    Memo1.Lines.Add('Signature ' + IntToStr(sig) + ' could not be verified.');
                  end;
                Memo1.Lines.Add(CkDkim__lastErrorText(verifier));
              end;
            if ((haveInfo = True) and (CkJsonObject_JsonTypeOf(info,'domain') = 1)) then
              begin
                Memo1.Lines.Add('Signing domain: ' + CkJsonObject__stringOf(info,'domain') + '  Selector: ' + CkJsonObject__stringOf(info,'selector'));
              end;
            sig := sig + 1;
          end;

      end;
    i := i + 1;
  end;

success := CkImap_Disconnect(imap);

CkImap_Dispose(imap);
CkMessageSet_Dispose(messages);
CkDkim_Dispose(verifier);
CkBinData_Dispose(message);
            CkJsonObject_Dispose(info);