Sample code for 30+ languages & platforms
PureBasic

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

PureBasic
IncludeFile "CkImap.pb"
IncludeFile "CkCert.pb"

Procedure ChilkatExample()

    success.i = 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.

    imap.i = CkImap::ckCreate()
    If imap.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkImap::setCkSsl(imap, 1)
    CkImap::setCkPort(imap, 993)

    success = CkImap::ckConnect(imap,"imap.example.com")
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        ProcedureReturn
    EndIf

    success = CkImap::ckLogin(imap,"user@example.com","myPassword")
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        ProcedureReturn
    EndIf

    ;  Get the server's TLS certificate for inspection or diagnostics.
    cert.i = CkCert::ckCreate()
    If cert.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkImap::ckGetServerCert(imap,cert)
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        CkCert::ckDispose(cert)
        ProcedureReturn
    EndIf

    Debug "Server certificate subject: " + CkCert::ckSubjectCN(cert)
    Debug "Issued by: " + CkCert::ckIssuerCN(cert)

    success = CkImap::ckDisconnect(imap)
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        CkCert::ckDispose(cert)
        ProcedureReturn
    EndIf



    CkImap::ckDispose(imap)
    CkCert::ckDispose(cert)


    ProcedureReturn
EndProcedure