Delphi DLL
Delphi DLL
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 Delphi DLL Downloads
var
success: Boolean;
mailman: HCkMailMan;
cert: HCkCert;
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 1 for
// the SMTP server's certificate, or 0 for the POP3 server's certificate.
mailman := CkMailMan_Create();
// Configure the SMTP server connection.
CkMailMan_putSmtpHost(mailman,'smtp.example.com');
CkMailMan_putSmtpPort(mailman,465);
CkMailMan_putSmtpSsl(mailman,True);
CkMailMan_putSmtpUsername(mailman,'user@example.com');
CkMailMan_putSmtpPassword(mailman,'myPassword');
// Establish the TLS connection.
success := CkMailMan_SmtpConnect(mailman);
if (success = False) then
begin
Memo1.Lines.Add(CkMailMan__lastErrorText(mailman));
Exit;
end;
// Get the certificate the SMTP server presented (useSmtp = 1).
cert := CkCert_Create();
success := CkMailMan_GetServerCert(mailman,True,cert);
if (success = False) then
begin
Memo1.Lines.Add(CkMailMan__lastErrorText(mailman));
Exit;
end;
Memo1.Lines.Add('Server certificate subject (CN): ' + CkCert__subjectCN(cert));
Memo1.Lines.Add('Issued by: ' + CkCert__issuerCN(cert));
success := CkMailMan_CloseSmtpConnection(mailman);
if (success = False) then
begin
Memo1.Lines.Add(CkMailMan__lastErrorText(mailman));
Exit;
end;
CkMailMan_Dispose(mailman);
CkCert_Dispose(cert);