Sample code for 30+ languages & platforms
Go

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

Go
    success := false

    //  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 := chilkat.NewImap()

    imap.SetSsl(true)
    imap.SetPort(993)

    success = imap.Connect("imap.example.com")
    if success == false {
        fmt.Println(imap.LastErrorText())
        imap.DisposeImap()
        return
    }

    success = imap.Login("user@example.com","myPassword")
    if success == false {
        fmt.Println(imap.LastErrorText())
        imap.DisposeImap()
        return
    }

    //  Get the server's TLS certificate for inspection or diagnostics.
    cert := chilkat.NewCert()
    success = imap.GetServerCert(cert)
    if success == false {
        fmt.Println(imap.LastErrorText())
        imap.DisposeImap()
        cert.DisposeCert()
        return
    }

    fmt.Println("Server certificate subject: ", cert.SubjectCN())
    fmt.Println("Issued by: ", cert.IssuerCN())

    success = imap.Disconnect()
    if success == false {
        fmt.Println(imap.LastErrorText())
        imap.DisposeImap()
        cert.DisposeCert()
        return
    }


    imap.DisposeImap()
    cert.DisposeCert()