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

Use an XML Certificate Vault with IMAP

See more IMAP Examples

Demonstrates the Chilkat Imap.UseCertVault method, which associates an XmlCertVault with the Imap object as a source of certificates and private keys for S/MIME operations. The only argument is the vault. Only one vault is associated at a time.

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

Background: An XmlCertVault is a portable container that can hold multiple certificates and keys in a single (optionally password-protected) XML file. It is a convenient way to ship the exact set of credentials an application needs without depending on the operating system's certificate stores. Associating a vault makes all of its contents available for S/MIME decryption and signature verification.

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.XmlCertVault;

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

procedure RunDemo;
var
  success: Boolean;
  imap: TImap;
  pfxPassword: string;
  vault: TXmlCertVault;

begin
  success := False;

  //  Demonstrates the Imap.UseCertVault method, which associates an XmlCertVault with the Imap
  //  object as a source of certificates and private keys for S/MIME operations.  The only
  //  argument is the XmlCertVault.

  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;

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

  //  Build a certificate vault and add a PFX to it.
  vault := TXmlCertVault.Create;
  success := vault.AddPfxFile('qa_data/smime.pfx',pfxPassword);
  if (success = False) then
    begin
      WriteLn(vault.LastErrorText);
      Exit;
    end;

  //  Associate the vault with the Imap object.  Only one vault is associated at a time.
  success := imap.UseCertVault(vault);
  if (success = False) then
    begin
      WriteLn(imap.LastErrorText);
      Exit;
    end;

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


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