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

Send an Email with a Digital Signature

See more Email Object Examples

Demonstrates the Chilkat Email.SendSigned property. Set it to true to have the email sent with a digital signature (the default is false). Signing also requires a certificate with access to its private key. This example loads that certificate (and its private key) from a PFX file with Cert.LoadPfxFile, supplies it via SetSigningCert, and enables signed sending.

Background: Signing is the mirror image of encrypting. To encrypt for a recipient you use their public certificate; to sign you use your own private key, and recipients verify with your public certificate. A signature does not hide the message — a signed email is still readable by anyone — but it proves who sent it and guarantees the content was not tampered with. A PFX (also called PKCS#12, .pfx or .p12) is a password-protected file that bundles a certificate together with its private key, making it a convenient single-file source for signing credentials.

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.Email,
  Chilkat.Cert;

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

procedure RunDemo;
var
  success: Boolean;
  email: TEmail;
  cert: TCert;

begin
  success := False;

  //  Demonstrates the Email.SendSigned property with a full signing setup.  Set SendSigned
  //  to true to have the email sent with a digital signature.  Signing requires a certificate
  //  with access to its private key, which we load here from a PFX (.pfx / .p12) file.

  email := TEmail.Create;
  email.Subject := 'Signed email';
  email.Body := 'This message will be sent with a digital signature.';
  email.From := 'alice@example.com';
  email.AddTo('Bob','bob@example.com');

  //  Load the signing certificate (including its private key) from a PFX file.
  //  The 2nd argument is the PFX password.
  cert := TCert.Create;
  success := cert.LoadPfxFile('qa_data/certs/signer.pfx','pfx_password');
  if (success = False) then
    begin
      WriteLn(cert.LastErrorText);
      Exit;
    end;

  //  Provide the signing certificate.
  success := email.SetSigningCert(cert);
  if (success = False) then
    begin
      WriteLn(email.LastErrorText);
      Exit;
    end;

  //  Request that the email be sent with a digital signature.
  email.SendSigned := True;

  WriteLn('SendSigned = ' + email.SendSigned);

  //  Note: The path "qa_data/certs/signer.pfx" is a relative local filesystem path,
  //  relative to the current working directory of the running application.


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