Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Get the SMTP or POP3 Server's TLS Certificate
See more SMTP Examples
Demonstrates the Chilkat MailMan.GetServerCert method, which gets the digital certificate presented by the SMTP or POP3 server for the current SSL/TLS connection and stores it in a Cert object. Pass true for the SMTP server's certificate or false for the POP3 server's. This example connects to the SMTP server and prints the certificate's subject and issuer.
Background: During a TLS handshake the server presents a certificate proving its identity, and the client validates it against trusted authorities. Retrieving that certificate lets you inspect it yourself — checking the subject, issuer, or expiration for auditing, logging, or certificate-pinning style checks. It must be called while a TLS connection is established, since the certificate belongs to that specific session.
Chilkat Pascal (Lazarus/Delphi) Downloads
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.GetServerCert method, which gets the digital certificate
// presented by the SMTP or POP3 server for the current SSL/TLS connection. Pass True for
// the SMTP server's certificate, or False for the POP3 server's certificate.
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';
// Establish the TLS connection.
success := mailman.SmtpConnect();
if (success = False) then
begin
WriteLn(mailman.LastErrorText);
Exit;
end;
// Get the certificate the SMTP server presented (useSmtp = True).
cert := TCert.Create;
success := mailman.GetServerCert(True,cert);
if (success = False) then
begin
WriteLn(mailman.LastErrorText);
Exit;
end;
WriteLn('Server certificate subject (CN): ' + cert.SubjectCN);
WriteLn('Issued by: ' + cert.IssuerCN);
success := mailman.CloseSmtpConnection();
if (success = False) then
begin
WriteLn(mailman.LastErrorText);
Exit;
end;
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.