Sample code for 30+ languages & platforms
Lazarus Pascal Requires Chilkat v11.0.0+

Decrypt an S/MIME email and verify its signature

See more Email Object Examples

Register the recipient’s PFX before opening signed-encrypted.eml. This workflow expects both encryption and a signature, so it checks both layers before reading the message. Cryptographic signature validity is one input to a signer-trust decision.

Chilkat Lazarus Pascal Downloads

Lazarus Pascal
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;
  signer: TCert;

begin
  success := False;

  //  Assumes Chilkat is unlocked. See Global Unlock Sample.

  email := TEmail.Create;
  success := email.AddPfxSourceFile('recipient.pfx','RECIPIENT_PASSWORD');
  if (success <> True) then
    begin
      WriteLn(email.LastErrorText);
      Exit;
    end;
  success := email.LoadEml('signed-encrypted.eml');
  if (success <> True) then
    begin
      WriteLn(email.LastErrorText);
      Exit;
    end;
  if ((email.ReceivedEncrypted <> True) or (email.Decrypted <> True)) then
    begin
      WriteLn('Expected a successfully decrypted S/MIME envelope.');
      Exit;
    end;
  if ((email.ReceivedSigned <> True) or (email.SignaturesValid <> True)) then
    begin
      WriteLn('Expected a valid signature inside the envelope.');
      Exit;
    end;
  //  This message has one signer. Examine its certificate after verification.
  signer := TCert.Create;
  success := email.LastSignerCert(0,signer);
  if (success <> True) then
    begin
      WriteLn(email.LastErrorText);
      Exit;
    end;
  WriteLn('Signer certificate: ' + signer.SubjectDN);
  WriteLn('Recovered subject: ' + email.Subject);
  WriteLn('Recovered attachments: ' + email.NumAttachments);


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