Sample code for 30+ languages & platforms
DataFlex

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

DataFlex
Use ChilkatAx-win32.pkg

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

    Move False To iSuccess

    //  Demonstrates the MailMan.GetServerCert method, which gets the digital certificate
    //  presented by the SMTP or POP3 server for the current SSL/TLS connection.  Pass True for
    //  the SMTP server's certificate, or False for the POP3 server's certificate.

    Get Create (RefClass(cComChilkatMailMan)) To hoMailman
    If (Not(IsComObjectCreated(hoMailman))) Begin
        Send CreateComObject of hoMailman
    End

    //  Configure the SMTP server connection.
    Set ComSmtpHost Of hoMailman To "smtp.example.com"
    Set ComSmtpPort Of hoMailman To 465
    Set ComSmtpSsl Of hoMailman To True
    Set ComSmtpUsername Of hoMailman To "user@example.com"
    Set ComSmtpPassword Of hoMailman To "myPassword"

    //  Establish the TLS connection.
    Get ComSmtpConnect Of hoMailman To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoMailman To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Get the certificate the SMTP server presented (useSmtp = True).
    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 hoMailman True vCert To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoMailman To sTemp1
        Showln sTemp1
        Procedure_Return
    End

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

    Get ComCloseSmtpConnection Of hoMailman To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoMailman To sTemp1
        Showln sTemp1
        Procedure_Return
    End



End_Procedure