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

Clear the Encryption Certificates of an Email

See more Email Object Examples

Demonstrates the Chilkat Email.ClearEncryptCerts method, which clears the internal list of explicitly specified certificates used when sending encrypted email — the certificates previously added with AddEncryptCert or SetEncryptCert. This example adds a recipient certificate and then clears the list.

Background: When encrypting to multiple recipients you register each one's certificate with AddEncryptCert. If an Email object is reused for different sets of recipients, those registered certificates would otherwise persist. ClearEncryptCerts resets that list so the next encrypted message is protected for exactly the intended recipients — a useful safeguard against accidentally encrypting for a stale recipient.

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 ClearEncryptCerts method, which clears the internal list of explicitly
  //  specified certificates used for sending encrypted email (the certificates added by
  //  AddEncryptCert / SetEncryptCert).

  email := TEmail.Create;

  //  Add a recipient certificate to the encryption cert list...
  cert := TCert.Create;
  success := cert.LoadFromFile('qa_data/certs/recipient.cer');
  if (success = False) then
    begin
      WriteLn(cert.LastErrorText);
      Exit;
    end;
  success := email.AddEncryptCert(cert);
  if (success = False) then
    begin
      WriteLn(email.LastErrorText);
      Exit;
    end;

  //  ...then clear all explicitly-specified encryption certificates.
  email.ClearEncryptCerts();

  WriteLn('Cleared the explicitly-specified encryption certificates.');

  //  Note: The path "qa_data/certs/recipient.cer" 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.