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

Add a PFX Source (BinData) for S/MIME Decryption

See more IMAP Examples

Demonstrates the Chilkat Imap.AddPfxSourceBd method, which adds PFX data held in a BinData as a source of certificates and private keys for S/MIME processing. The first argument is the BinData and the second is the PFX password. Call it once for each additional source.

Note: The certificate path is relative to the application's current working directory. Supply the path to your own certificate file.

Background: When downloading encrypted (S/MIME) email, Chilkat needs the recipient's private key to decrypt it. Adding one or more PFX sources gives Chilkat a pool of keys to search; the matching key is used automatically as messages are fetched. The BinData form is handy when the PFX comes from memory — a database blob or a downloaded buffer — rather than a file on disk. On Windows and macOS the system certificate stores are also searched automatically.

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.Imap,
  Chilkat.BinData;

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

procedure RunDemo;
var
  success: Boolean;
  imap: TImap;
  bdPfx: TBinData;
  pfxPassword: string;

begin
  success := False;

  //  Demonstrates the Imap.AddPfxSourceBd method, which adds PFX data (from a BinData) as a
  //  source of certificates and private keys for S/MIME decryption.  The 1st argument is the
  //  BinData containing the PFX, and the 2nd is the PFX password.

  imap := TImap.Create;

  imap.Ssl := True;
  imap.Port := 993;

  success := imap.Connect('imap.example.com');
  if (success = False) then
    begin
      WriteLn(imap.LastErrorText);
      Exit;
    end;
  success := imap.Login('user@example.com','myPassword');
  if (success = False) then
    begin
      WriteLn(imap.LastErrorText);
      Exit;
    end;

  //  Load the PFX data into a BinData.
  bdPfx := TBinData.Create;
  success := bdPfx.LoadFile('qa_data/smime.pfx');
  if (success = False) then
    begin
      WriteLn(bdPfx.LastErrorText);
      Exit;
    end;

  //  The PFX password is not hard-coded in source.  Insert code here to obtain it from an
  //  interactive prompt, environment variable, or a secrets vault.

  //  Add the PFX as a source for S/MIME certificates and private keys.  Call once per source.
  success := imap.AddPfxSourceBd(bdPfx,pfxPassword);
  if (success = False) then
    begin
      WriteLn(imap.LastErrorText);
      Exit;
    end;

  //  Messages downloaded after this are automatically decrypted when a matching key is found.

  success := imap.Disconnect();
  if (success = False) then
    begin
      WriteLn(imap.LastErrorText);
      Exit;
    end;


  imap.Free;
  bdPfx.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.