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

Use a TLS Client Certificate

See more SMTP Examples

Demonstrates the Chilkat MailMan.SetSslClientCert method, which sets the client-side certificate to use for SSL/TLS connections. This is typically not required — most SSL/TLS connections authenticate the server only. This example loads a client certificate from a PFX and uses it when connecting.

Background: In an ordinary TLS handshake only the server presents a certificate, and the client proves who it is later with a password. Mutual TLS adds a second leg: the client also presents a certificate, so it is authenticated cryptographically at the transport layer. Some corporate or high-security mail servers require this. Because the client must prove possession of the key, the certificate needs its private key — hence loading from a PFX.

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

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

procedure RunDemo;
var
  success: Boolean;
  mailman: TMailMan;
  cert: TCert;

begin
  success := False;

  //  Demonstrates the MailMan.SetSslClientCert method, which sets the client-side certificate
  //  to use for SSL/TLS connections.  This is typically not required -- most SSL/TLS
  //  connections authenticate the server only.

  mailman := TMailMan.Create;

  //  Configure the SMTP server connection.
  mailman.SmtpHost := 'smtp.example.com';
  mailman.SmtpPort := 465;
  mailman.SmtpSsl := True;
  mailman.SmtpUsername := 'user@example.com';
  mailman.SmtpPassword := 'myPassword';

  //  Load the client certificate (with its private key) from a PFX file.
  cert := TCert.Create;
  success := cert.LoadPfxFile('qa_data/certs/client.pfx','pfx_password');
  if (success = False) then
    begin
      WriteLn(cert.LastErrorText);
      Exit;
    end;

  //  Use this certificate to identify the client during the TLS handshake.
  success := mailman.SetSslClientCert(cert);
  if (success = False) then
    begin
      WriteLn(mailman.LastErrorText);
      Exit;
    end;

  success := mailman.VerifySmtpLogin();
  if (success = False) then
    begin
      WriteLn(mailman.LastErrorText);
      Exit;
    end;

  WriteLn('Connected using a TLS client certificate.');

  //  Note: The path "qa_data/certs/client.pfx" is a relative local filesystem path,
  //  relative to the current working directory of the running application.


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