Lazarus Pascal Requires Chilkat v11.0.0+
Lazarus Pascal
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 Lazarus Pascal Downloads
program ChilkatDemo;
// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
SysUtils,
CkDllLoader,
Chilkat.BinData,
Chilkat.Dkim,
Chilkat.Imap,
Chilkat.JsonObject,
Chilkat.MessageSet;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
imap: TImap;
messages: TMessageSet;
i: Integer;
verifier: TDkim;
message: TBinData;
uid: LongWord;
count: Integer;
sig: Integer;
info: TJsonObject;
haveInfo: Boolean;
begin
success := False;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
imap := TImap.Create;
imap.Ssl := True;
imap.Port := 993;
imap.RequireSslCertVerify := True;
imap.PeekMode := True;
imap.AutoDownloadAttachments := True;
success := imap.Connect('imap.example.com');
if (success <> True) then
begin
WriteLn(imap.LastErrorText);
Exit;
end;
success := imap.Login('IMAP_USERNAME','IMAP_PASSWORD');
if (success <> True) then
begin
WriteLn(imap.LastErrorText);
Exit;
end;
success := imap.SelectMailbox('INBOX');
if (success <> True) then
begin
WriteLn(imap.LastErrorText);
Exit;
end;
// Select the first ten sequence positions, then fetch by stable UID.
messages := TMessageSet.Create;
success := imap.QueryMbx('1:10',True,messages);
if (success <> True) then
begin
WriteLn(imap.LastErrorText);
Exit;
end;
i := 0;
verifier := TDkim.Create;
message := TBinData.Create;
while (i < messages.Count) do
begin
uid := messages.GetId(i);
WriteLn('Message UID ' + uid);
success := imap.FetchSingleBd(uid,True,message);
if (success <> True) then
begin
WriteLn(imap.LastErrorText);
end
else
begin
count := verifier.NumDkimSigs(message);
if (count = 0) then
begin
WriteLn('No DKIM signatures.');
end;
sig := 0;
while (sig < count) do
begin
success := verifier.DkimVerify(sig,message);
// Read diagnostics before the next verification replaces them.
info := TJsonObject.Create;
haveInfo := info.Load(verifier.VerifyInfo);
if (success = True) then
begin
WriteLn('Signature ' + sig + ' verified.');
end
else
begin
if ((haveInfo = True) and (info.StringOfEquals('bodyHashVerified','no',True) = True)) then
begin
WriteLn('Signature ' + sig + ': the body hash differs.');
end
else
begin
WriteLn('Signature ' + sig + ' could not be verified.');
end;
WriteLn(verifier.LastErrorText);
end;
if ((haveInfo = True) and (info.JsonTypeOf('domain') = 1)) then
begin
WriteLn('Signing domain: ' + info.StringOf('domain') + ' Selector: ' + info.StringOf('selector'));
end;
sig := sig + 1;
end;
end;
i := i + 1;
end;
success := imap.Disconnect();
imap.Free;
messages.Free;
verifier.Free;
message.Free;
info.Free;
end;
// ---------------------------------------------------------------------------
begin
try
RunDemo;
except
on E: Exception do
WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
end;
WriteLn;
{$IFDEF MSWINDOWS}
WriteLn('Press Enter to exit...');
ReadLn;
{$ENDIF}
end.