Sample code for 30+ languages & platforms
PowerBuilder

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 PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Mailman
oleobject loo_Cert

li_Success = 0

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

loo_Mailman = create oleobject
li_rc = loo_Mailman.ConnectToNewObject("Chilkat.MailMan")
if li_rc < 0 then
    destroy loo_Mailman
    MessageBox("Error","Connecting to COM object failed")
    return
end if

//  Configure the SMTP server connection.
loo_Mailman.SmtpHost = "smtp.example.com"
loo_Mailman.SmtpPort = 465
loo_Mailman.SmtpSsl = 1
loo_Mailman.SmtpUsername = "user@example.com"
loo_Mailman.SmtpPassword = "myPassword"

//  Establish the TLS connection.
li_Success = loo_Mailman.SmtpConnect()
if li_Success = 0 then
    Write-Debug loo_Mailman.LastErrorText
    destroy loo_Mailman
    return
end if

//  Get the certificate the SMTP server presented (useSmtp = 1).
loo_Cert = create oleobject
li_rc = loo_Cert.ConnectToNewObject("Chilkat.Cert")

li_Success = loo_Mailman.GetServerCert(1,loo_Cert)
if li_Success = 0 then
    Write-Debug loo_Mailman.LastErrorText
    destroy loo_Mailman
    destroy loo_Cert
    return
end if

Write-Debug "Server certificate subject (CN): " + loo_Cert.SubjectCN
Write-Debug "Issued by: " + loo_Cert.IssuerCN

li_Success = loo_Mailman.CloseSmtpConnection()
if li_Success = 0 then
    Write-Debug loo_Mailman.LastErrorText
    destroy loo_Mailman
    destroy loo_Cert
    return
end if



destroy loo_Mailman
destroy loo_Cert