Sample code for 30+ languages & platforms
Android™

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 Android™ Downloads

Android™
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;

import android.app.Activity;
import com.chilkatsoft.*;

import android.widget.TextView;
import android.os.Bundle;

public class SimpleActivity extends Activity {

  private static final String TAG = "Chilkat";

  // Called when the activity is first created.
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    boolean success = 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.

    CkMailMan mailman = new CkMailMan();

    //  Configure the SMTP server connection.
    mailman.put_SmtpHost("smtp.example.com");
    mailman.put_SmtpPort(465);
    mailman.put_SmtpSsl(true);
    mailman.put_SmtpUsername("user@example.com");
    mailman.put_SmtpPassword("myPassword");

    //  Establish the TLS connection.
    success = mailman.SmtpConnect();
    if (success == false) {
        Log.i(TAG, mailman.lastErrorText());
        return;
        }

    //  Get the certificate the SMTP server presented (useSmtp = true).
    CkCert cert = new CkCert();
    success = mailman.GetServerCert(true,cert);
    if (success == false) {
        Log.i(TAG, mailman.lastErrorText());
        return;
        }

    Log.i(TAG, "Server certificate subject (CN): " + cert.subjectCN());
    Log.i(TAG, "Issued by: " + cert.issuerCN());

    success = mailman.CloseSmtpConnection();
    if (success == false) {
        Log.i(TAG, mailman.lastErrorText());
        return;
        }


  }

  static {
      System.loadLibrary("chilkat");

      // Note: If the incorrect library name is passed to System.loadLibrary,
      // then you will see the following error message at application startup:
      //"The application <your-application-name> has stopped unexpectedly. Please try again."
  }
}