Sample code for 30+ languages & platforms
Lazarus Pascal Requires Chilkat v11.0.0+

Sign an email and send the signed bytes over SMTP

See more DKIM Examples

Compose the email, render it to MIME once, add its DKIM signature, and submit those same bytes to an SMTP server. Put blind-copy recipients in the SMTP envelope only so their addresses stay out of the visible headers.

Use an authorized SMTP account and your own sender domain. Publish the selector’s matching public key first. If your relay rewrites signed content, have it apply the signature after its final edits. A successful SMTP submission means the relay accepted the message. A recipient can then verify it with the public key in DNS.

Chilkat Lazarus Pascal Downloads

Lazarus Pascal
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.MailMan,
  Chilkat.BinData,
  Chilkat.PrivateKey,
  Chilkat.Email,
  Chilkat.Dkim;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  mail: TMailMan;
  email: TEmail;
  message: TBinData;
  privateKey: TPrivateKey;
  signer: TDkim;
  recipients: string;

begin
  success := False;

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

  mail := TMailMan.Create;
  mail.SmtpHost := 'smtp.example.com';
  mail.SmtpPort := 465;
  mail.SmtpSsl := True;
  mail.RequireSslCertVerify := True;
  mail.SmtpUsername := 'SMTP_USERNAME';
  mail.SmtpPassword := 'SMTP_PASSWORD';
  email := TEmail.Create;
  email.From := 'Workshop team <updates@example.com>';
  email.Subject := 'Workshop registration';
  email.Body := 'Registration opens on Monday.';
  success := email.AddTo('Newsletter editor','editor@example.net');
  if (success <> True) then
    begin
      WriteLn(email.LastErrorText);
      Exit;
    end;
  //  Render after all headers, recipients, and content are final.
  message := TBinData.Create;
  success := mail.RenderToMimeBd(email,message);
  if (success <> True) then
    begin
      WriteLn(mail.LastErrorText);
      Exit;
    end;
  //  Supply your RSA signing key. For an encrypted PEM, get its password from your credential store.
  privateKey := TPrivateKey.Create;
  success := privateKey.LoadAnyFormatFile('dkim-private.pem','');
  if (success <> True) then
    begin
      WriteLn(privateKey.LastErrorText);
      Exit;
    end;
  signer := TDkim.Create;
  signer.DkimDomain := 'example.com';
  signer.DkimSelector := 'mail2026';
  signer.DkimAlg := 'rsa-sha256';
  success := signer.SetDkimPrivateKey(privateKey);
  if (success <> True) then
    begin
      WriteLn(signer.LastErrorText);
      Exit;
    end;
  success := signer.DkimSign(message);
  if (success <> True) then
    begin
      WriteLn(signer.LastErrorText);
      Exit;
    end;
  //  archive@example.com is an envelope-only blind copy.
  recipients := 'editor@example.net,archive@example.com';
  success := mail.SendMimeBd('updates@example.com',recipients,message);
  if (success <> True) then
    begin
      WriteLn(mail.LastErrorText);
      Exit;
    end;
  WriteLn('SMTP relay accepted the signed message.');
  success := mail.CloseSmtpConnection();
  if (success <> True) then
    begin
      WriteLn('The SMTP session did not close cleanly.');
    end;


  mail.Free;
  email.Free;
  message.Free;
  privateKey.Free;
  signer.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.