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

Delphi ActiveX
var
success: Integer;
imap: TChilkatImap;
messages: TMessageSet;
i: Integer;
verifier: TChilkatDkim;
message: TChilkatBinData;
uid: Cardinal;
count: Integer;
sig: Integer;
info: TChilkatJsonObject;
haveInfo: Integer;

begin
success := 0;

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

imap := TChilkatImap.Create(Self);
imap.Ssl := 1;
imap.Port := 993;
imap.RequireSslCertVerify := 1;
imap.PeekMode := 1;
imap.AutoDownloadAttachments := 1;
success := imap.Connect('imap.example.com');
if (success <> 1) then
  begin
    Memo1.Lines.Add(imap.LastErrorText);
    Exit;
  end;
success := imap.Login('IMAP_USERNAME','IMAP_PASSWORD');
if (success <> 1) then
  begin
    Memo1.Lines.Add(imap.LastErrorText);
    Exit;
  end;
success := imap.SelectMailbox('INBOX');
if (success <> 1) then
  begin
    Memo1.Lines.Add(imap.LastErrorText);
    Exit;
  end;
//  Select the first ten sequence positions, then fetch by stable UID.
messages := TMessageSet.Create(Self);
success := imap.QueryMbx('1:10',1,messages.ControlInterface);
if (success <> 1) then
  begin
    Memo1.Lines.Add(imap.LastErrorText);
    Exit;
  end;
i := 0;
verifier := TChilkatDkim.Create(Self);
message := TChilkatBinData.Create(Self);
while (i < messages.Count) do
  begin
    uid := messages.GetId(i);
    Memo1.Lines.Add('Message UID ' + IntToStr(uid));
    success := imap.FetchSingleBd(uid,1,message.ControlInterface);
    if (success <> 1) then
      begin
        Memo1.Lines.Add(imap.LastErrorText);
      end
    else
      begin
        count := verifier.NumDkimSigs(message.ControlInterface);
        if (count = 0) then
          begin
            Memo1.Lines.Add('No DKIM signatures.');
          end;
        sig := 0;
        while (sig < count) do
          begin
            success := verifier.DkimVerify(sig,message.ControlInterface);
            //  Read diagnostics before the next verification replaces them.
            info := TChilkatJsonObject.Create(Self);
            haveInfo := info.Load(verifier.VerifyInfo);
            if (success = 1) then
              begin
                Memo1.Lines.Add('Signature ' + IntToStr(sig) + ' verified.');
              end
            else
              begin
                if ((haveInfo = 1) and (info.StringOfEquals('bodyHashVerified','no',1) = 1)) 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(verifier.LastErrorText);
              end;
            if ((haveInfo = 1) and (info.JsonTypeOf('domain') = 1)) then
              begin
                Memo1.Lines.Add('Signing domain: ' + info.StringOf('domain') + '  Selector: ' + info.StringOf('selector'));
              end;
            sig := sig + 1;
          end;

      end;
    i := i + 1;
  end;

success := imap.Disconnect();