Sample code for 30+ languages & platforms
PowerBuilder

Get the IMAP Server's TLS Certificate

See more IMAP Examples

Demonstrates the Chilkat Imap.GetServerCert method, which retrieves the certificate the IMAP server presented on the current (or most recent) TLS connection. The only argument is a Cert object that receives the certificate. This example prints the subject and issuer common names.

Background: Chilkat already validates the server certificate as part of the TLS handshake, but this method lets an application inspect it directly — to log the issuer, display connection details, implement certificate pinning, or apply an application-specific trust policy. The Cert object exposes the full certificate, including subject, issuer, validity dates, and the public key.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Imap
oleobject loo_Cert

li_Success = 0

//  Demonstrates the Imap.GetServerCert method, which retrieves the certificate presented by the
//  IMAP server on the current TLS connection.  The only argument is a Cert object that receives
//  the certificate.

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

loo_Imap.Ssl = 1
loo_Imap.Port = 993

li_Success = loo_Imap.Connect("imap.example.com")
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    return
end if

li_Success = loo_Imap.Login("user@example.com","myPassword")
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    return
end if

//  Get the server's TLS certificate for inspection or diagnostics.
loo_Cert = create oleobject
li_rc = loo_Cert.ConnectToNewObject("Chilkat.Cert")

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

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

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



destroy loo_Imap
destroy loo_Cert