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

Set the Signing Certificate for an Email

See more Email Object Examples

Demonstrates the Chilkat Email.SetSigningCert method, which sets the certificate (with access to its private key) used to create a digital signature when sending signed email. This example loads the signing certificate from a PFX, sets it, and enables signed sending.

Background: Signing uses your own private key (the opposite of encrypting, which uses the recipient's public key), so the signing certificate must include private-key access. A PFX (PKCS#12) file bundles the certificate and its private key together, which is why it is loaded here with LoadPfxFile. If the certificate and key are in separate files, use SetSigningCert2 instead.

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 SetSigningCert method, which sets the certificate (with access to its
  //  private key) used for creating a digital signature when sending signed email.

  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 a signing certificate together with its private key from a PFX file.
  cert := TCert.Create;
  success := cert.LoadPfxFile('qa_data/certs/signer.pfx','pfx_password');
  if (success = False) then
    begin
      WriteLn(cert.LastErrorText);
      Exit;
    end;

  //  Set the certificate used to create the signature.
  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('Signing certificate set.');

  //  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.