Sample code for 30+ languages & platforms
DataFlex

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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoImap
    Variant vCert
    Handle hoCert
    String sTemp1

    Move False To iSuccess

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

    Get Create (RefClass(cComChilkatImap)) To hoImap
    If (Not(IsComObjectCreated(hoImap))) Begin
        Send CreateComObject of hoImap
    End

    Set ComSsl Of hoImap To True
    Set ComPort Of hoImap To 993

    Get ComConnect Of hoImap "imap.example.com" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComLogin Of hoImap "user@example.com" "myPassword" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Get the server's TLS certificate for inspection or diagnostics.
    Get Create (RefClass(cComChilkatCert)) To hoCert
    If (Not(IsComObjectCreated(hoCert))) Begin
        Send CreateComObject of hoCert
    End
    Get pvComObject of hoCert to vCert
    Get ComGetServerCert Of hoImap vCert To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComSubjectCN Of hoCert To sTemp1
    Showln "Server certificate subject: " sTemp1
    Get ComIssuerCN Of hoCert To sTemp1
    Showln "Issued by: " sTemp1

    Get ComDisconnect Of hoImap To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End



End_Procedure