Sample code for 30+ languages & platforms
Tcl

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

Tcl

load ./chilkat.dll

set success 0

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

set mailman [new_CkMailMan]

#  Configure the SMTP server connection.
CkMailMan_put_SmtpHost $mailman "smtp.example.com"
CkMailMan_put_SmtpPort $mailman 465
CkMailMan_put_SmtpSsl $mailman 1
CkMailMan_put_SmtpUsername $mailman "user@example.com"
CkMailMan_put_SmtpPassword $mailman "myPassword"

#  Establish the TLS connection.
set success [CkMailMan_SmtpConnect $mailman]
if {$success == 0} then {
    puts [CkMailMan_lastErrorText $mailman]
    delete_CkMailMan $mailman
    exit
}

#  Get the certificate the SMTP server presented (useSmtp = cktrue).
set cert [new_CkCert]

set success [CkMailMan_GetServerCert $mailman 1 $cert]
if {$success == 0} then {
    puts [CkMailMan_lastErrorText $mailman]
    delete_CkMailMan $mailman
    delete_CkCert $cert
    exit
}

puts "Server certificate subject (CN): [CkCert_subjectCN $cert]"
puts "Issued by: [CkCert_issuerCN $cert]"

set success [CkMailMan_CloseSmtpConnection $mailman]
if {$success == 0} then {
    puts [CkMailMan_lastErrorText $mailman]
    delete_CkMailMan $mailman
    delete_CkCert $cert
    exit
}


delete_CkMailMan $mailman
delete_CkCert $cert