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

Send an Email S/MIME Encrypted

See more Email Object Examples

Demonstrates the Chilkat Email.SendEncrypted property. Set it to true to have the email sent S/MIME encrypted (the default is false). Setting this property alone is not enough — encryption requires one or more recipient certificates, supplied with AddEncryptCert or SetEncryptCert. This example loads a recipient certificate, registers it, and enables encrypted sending.

Background: To encrypt a message for someone, you need their public certificate — which is why only a .cer file (no private key) is required here. The message is scrambled so that only the holder of the matching private key, i.e. the intended recipient, can read it. When there are multiple recipients, you add each one's certificate, and Chilkat encrypts the one-time content key separately for each so they can all decrypt the same message.

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.SendEncrypted property.  Set to true to have the email sent
  //  S/MIME encrypted.  Encryption also requires one or more recipient certificates,
  //  supplied via AddEncryptCert or SetEncryptCert.  The default is false.

  email := TEmail.Create;
  email.Subject := 'Encrypted email';
  email.Body := 'This message will be sent S/MIME encrypted.';
  email.From := 'alice@example.com';
  email.AddTo('Bob','bob@example.com');

  //  Load the recipient's certificate (only the public key is needed to encrypt).
  cert := TCert.Create;
  success := cert.LoadFromFile('qa_data/certs/recipient.cer');
  if (success = False) then
    begin
      WriteLn(cert.LastErrorText);
      Exit;
    end;

  //  Supply the recipient certificate used for encryption.
  success := email.AddEncryptCert(cert);
  if (success = False) then
    begin
      WriteLn(email.LastErrorText);
      Exit;
    end;

  //  Request that the email be sent encrypted.
  email.SendEncrypted := True;

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

  //  Note: Paths such as "qa_data/..." are relative local filesystem paths,
  //  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.