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

Add S/MIME Signature using PFX

See more MIME Examples

Add a digital signature to a MIME message using the certificate + private key from a PFX file.

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.Cert,
  Chilkat.Mime;

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

procedure RunDemo;
var
  success: Boolean;
  mime: TMime;
  cert: TCert;
  pfxFilepath: string;
  pfxPassword: string;

begin
  success := False;

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

  mime := TMime.Create;

  //  Load a PFX file into a certificate object.
  cert := TCert.Create;
  pfxFilepath := 'pfxFiles/something.pfx';
  pfxPassword := 'secret';
  success := cert.LoadPfxFile(pfxFilepath,pfxPassword);
  if (success = False) then
    begin
      WriteLn(cert.LastErrorText);
      Exit;
    end;

  success := mime.SetBodyFromPlainText('This is the plain-text MIME body.');

  mime.Charset := 'utf-8';
  mime.Encoding := 'quoted-printable';

  //  Sign the MIME (adds a PKCS7 detached signature)
  success := mime.AddDetachedSignature(cert);
  if (success = False) then
    begin
      WriteLn(mime.LastErrorText);
      Exit;
    end;

  //  Save the S/MIME to a file.
  success := mime.SaveMime('/temp/signedMime.txt');
  if (success = False) then
    begin
      WriteLn(mime.LastErrorText);
      Exit;
    end;

  WriteLn('success!');


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