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

Load Signed and/or Encrypted Email and Unwrap Security Layers

See more Email Object Examples

The LoadEml method loads an email (.eml file containing the MIME of an email) and automatically unwraps the digital signature and encryption security layers. A program can then examine the properties of the email object to see what was unwrapped.

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;

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

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

begin
  success := False;

  email := TEmail.Create;

  //  Provide PFX/.p12 files for any certs+keys needed for decryption.
  //  Signature verification does not need anything extra.  
  //  (Also, Chilkat will automatically find and use pre-installed certs + private keys needed for decryption
  //  on a Windows system.)
  success := email.AddPfxSourceFile('/pfxFiles/certs_and_keys_1.pfx','pfxPassword1');
  if (success <> True) then
    begin
      WriteLn(email.LastErrorText);
      Exit;
    end;
  success := email.AddPfxSourceFile('/pfxFiles/certs_and_keys_2.pfx','pfxPassword2');
  if (success <> True) then
    begin
      WriteLn(email.LastErrorText);
      Exit;
    end;
  //  ...

  //  Load a potentially signed and/or encrypted email.
  success := email.LoadEml('someDir/someEmail.eml');

  //  If the email was signed and/or encrypted, it was "unwrapped", i.e.
  //  the email is already decrypted and in a state as if it were never signed or encrypted.

  //  You may check to see if the email was received encrypted or signed, and if so,
  //  whether it was successfully unwrapped and who signed or encrypted it:
  if (email.ReceivedEncrypted = True) then
    begin

      WriteLn('This email was encrypted.');
      if (email.Decrypted = True) then
        begin
          WriteLn('This email was successfully decrypted.  It was encrypted by:');
          WriteLn(email.EncryptedBy);
        end
      else
        begin
          WriteLn('This email was not decrypted.');
        end;
    end;
  if (email.ReceivedSigned = True) then
    begin

      WriteLn('This email was signed.');
      if (email.SignaturesValid = True) then
        begin
          WriteLn('The signature was verified.  It was signed by:');
          WriteLn(email.SignedBy);
        end
      else
        begin
          WriteLn('The signature verification failed.');
        end;
    end;

  //  At this point, the contents of the email, including attachments, can be accessed normally..


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