Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

DKIM Signature using Windows Current User Certificate Store

See more DKIM / DomainKey Examples

This is a Windows-specific example to load a certificate from the Current User (registry-based) certificate store, and then use the certificate's associated private key for a DKIM signature.

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
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.PrivateKey,
  Chilkat.Cert,
  Chilkat.Dkim;

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

procedure RunDemo;
var
  success: Boolean;
  cert: TCert;
  privKey: TPrivateKey;
  dkim: TDkim;

begin
  success := False;

  //  This example requires the Chilkat API to have been previously unlocked.
  //  See Global Unlock Sample for sample code.

  cert := TCert.Create;

  //  The LoadByCommonName method searches the Windows Local Machine and Current User 
  //  registry-based certificate stores for a certificate having the common name specified. 
  //  If found, the certificate is loaded and ready for use.
  success := cert.LoadByCommonName('My Certificate ABC');
  if (success = False) then
    begin
      WriteLn(cert.LastErrorText);
      Exit;
    end;

  //  The certificate must have an associated private key installed, and it must be a
  //  private key that has been marked "exportable" when it was originally installed.
  if (not cert.HasPrivateKey()) then
    begin
      WriteLn('This certificate does not have a private key available.');
      Exit;
    end;

  privKey := TPrivateKey.Create;
  success := cert.GetPrivateKey(privKey);
  if (success = False) then
    begin
      WriteLn(cert.LastErrorText);
      Exit;
    end;

  dkim := TDkim.Create;

  //  Load the private key into the DKIM object:
  success := dkim.SetDkimPrivateKey(privKey);
  if (success = False) then
    begin
      WriteLn(dkim.LastErrorText);
      Exit;
    end;

  //  The private key has been loaded into the DKIM object.  See the other DKIM
  //  examples for guidance on how to create a DKIM signature...


  cert.Free;
  privKey.Free;
  dkim.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.