Sample code for 30+ languages & platforms
Swift

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

Swift

func chilkatTest() {
    var success: Bool = false

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

    let mailman = CkoMailMan()!

    //  Configure the SMTP server connection.
    mailman.smtpHost = "smtp.example.com"
    mailman.smtpPort = 465
    mailman.smtpSsl = true
    mailman.smtpUsername = "user@example.com"
    mailman.smtpPassword = "myPassword"

    //  Establish the TLS connection.
    success = mailman.smtpConnect()
    if success == false {
        print("\(mailman.lastErrorText!)")
        return
    }

    //  Get the certificate the SMTP server presented (useSmtp = true).
    let cert = CkoCert()!
    success = mailman.getServerCert(useSmtp: true, cert: cert)
    if success == false {
        print("\(mailman.lastErrorText!)")
        return
    }

    print("Server certificate subject (CN): \(cert.subjectCN!)")
    print("Issued by: \(cert.issuerCN!)")

    success = mailman.closeSmtpConnection()
    if success == false {
        print("\(mailman.lastErrorText!)")
        return
    }


}