Sample code for 30+ languages & platforms
Android™

Use a PFX TLS Client Certificate

See more SMTP Examples

Demonstrates the Chilkat MailMan.SetSslClientCertPfx method, which sets the client-side certificate for SSL/TLS connections, loading it from a PFX/PKCS#12 file. The first argument is the path to the PFX file and the second is its password. This example loads a client certificate directly from a PFX.

Background: This is the convenience form of SetSslClientCert: rather than loading a Cert object first, you point straight at the PFX file and password. A PFX (PKCS#12) bundles the certificate with its private key — exactly what mutual TLS requires, since the client must prove possession of the key during the handshake. It is the usual client-credential format on Windows.

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.SetSslClientCertPfx method, which sets the client-side
    //  certificate for SSL/TLS connections, loading it from a PFX/PKCS#12 file.  The 1st argument
    //  is the PFX path and the 2nd is the PFX password.

    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");

    //  Load the client certificate directly from a PFX file.
    success = mailman.SetSslClientCertPfx("qa_data/certs/client.pfx","pfx_password");
    if (success == false) {
        Log.i(TAG, mailman.lastErrorText());
        return;
        }

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

    Log.i(TAG, "Connected using a PFX TLS client certificate.");

    //  Note: The path "qa_data/certs/client.pfx" is a relative local filesystem path,
    //  relative to the current working directory of the running application.

  }

  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."
  }
}