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

POP3 Verify Signed (S/MIME) Email

Demonstrates how to download and verify digitally signed S/MIME email.

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.StringTable,
  Chilkat.Email,
  Chilkat.Cert,
  Chilkat.MailMan;

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

procedure RunDemo;
var
  success: Boolean;
  mailman: TMailMan;
  stUidls: TStringTable;
  email: TEmail;
  cert: TCert;
  count: Integer;
  i: Integer;

begin
  success := False;

  //  This example requires the Chilkat API to have been previously unlocked.
  //  See Global Unlock Sample for sample code.

  mailman := TMailMan.Create;

  //  Set the POP3 server's hostname
  mailman.MailHost := 'pop.example.com';

  //  Set the POP3 login/password.
  mailman.PopUsername := 'myLogin';
  mailman.PopPassword := 'myPassword';

  stUidls := TStringTable.Create;
  success := mailman.FetchUidls(stUidls);
  if (success = False) then
    begin
      WriteLn(mailman.LastErrorText);
      Exit;
    end;

  email := TEmail.Create;
  cert := TCert.Create;

  count := stUidls.Count;
  i := 0;
  while i < count do
    begin
      //  Download the full email.
      success := mailman.FetchByUidl(stUidls.StringAt(i),False,0,email);
      if (success = False) then
        begin
          WriteLn(mailman.LastErrorText);
          Exit;
        end;

      WriteLn(i);
      WriteLn('From: ' + email.From);
      WriteLn('Subject: ' + email.Subject);

      //  The security layers of signed and/or encrypted emails
      //  are automatically "unwrapped" when loaded into
      //  a Chilkat email object.
      //  An application only needs to check to see if an email
      //  was received signed or encrypted, and then examine
      //  the success/failure.  For example:
      if (email.ReceivedSigned = True) then
        begin

          WriteLn('This email was signed.');

          //  Check to see if the signatures were verified.
          if (email.SignaturesValid = True) then
            begin
              WriteLn('Digital signature(s) verified.');
              WriteLn('Signer: ' + email.SignedBy);

              success := email.LastSignerCert(0,cert);
              if (success = False) then
                begin
                  WriteLn(email.LastErrorText);
                  Exit;
                end;

              WriteLn('Signing cert: ' + cert.SubjectCN);
            end;

        end
      else
        begin
          WriteLn('Digital signature verification failed.');
        end;

      i := i + 1;
    end;

  mailman.Pop3EndSession();


  mailman.Free;
  stUidls.Free;
  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.