Sample code for 30+ languages & platforms
Android™

Add a PFX Source File for S/MIME Decryption

See more IMAP Examples

Demonstrates the Chilkat Imap.AddPfxSourceFile method, which adds a PFX file as a source of certificates and private keys for S/MIME processing. The first argument is the PFX file path and the second is its password. Call it once for each additional source.

Note: The certificate path is relative to the application's current working directory. Supply the path to your own certificate file.

Background: This is the file-based form of AddPfxSourceBd. Registering the private keys needed to decrypt S/MIME mail lets Chilkat decrypt messages transparently during fetch. Add several sources if recipients' keys are spread across multiple PFX files. On Windows and macOS the system stores (and Keychain) are searched automatically in addition to the sources you add.

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 Imap.AddPfxSourceFile method, which adds a PFX file as a source of
    //  certificates and private keys for S/MIME decryption.  The 1st argument is the PFX file path
    //  and the 2nd is its password.

    CkImap imap = new CkImap();

    imap.put_Ssl(true);
    imap.put_Port(993);

    success = imap.Connect("imap.example.com");
    if (success == false) {
        Log.i(TAG, imap.lastErrorText());
        return;
        }

    success = imap.Login("user@example.com","myPassword");
    if (success == false) {
        Log.i(TAG, imap.lastErrorText());
        return;
        }

    //  The PFX password is not hard-coded in source.  Insert code here to obtain it from an
    //  interactive prompt, environment variable, or a secrets vault.
    String pfxPassword;

    //  Add the PFX file as a source for S/MIME certificates and private keys.  Call once per source.
    success = imap.AddPfxSourceFile("qa_data/smime.pfx",pfxPassword);
    if (success == false) {
        Log.i(TAG, imap.lastErrorText());
        return;
        }

    //  Messages downloaded after this are automatically decrypted when a matching key is found.

    success = imap.Disconnect();
    if (success == false) {
        Log.i(TAG, imap.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."
  }
}