Sample code for 30+ languages & platforms
Android™

Set the Decryption Certificate for an Email

See more Email Object Examples

Demonstrates the Chilkat Email.SetDecryptCert method, which explicitly provides a certificate (with access to its private key) for decrypting a received encrypted email. Set the certificate before loading the encrypted message so Chilkat can decrypt it automatically. This example loads the certificate from a PFX, sets it, and then loads an encrypted email.

Background: Decrypting S/MIME email requires the recipient's private key. On Windows and macOS, Chilkat can find an installed certificate automatically, but when the key lives in a standalone PFX file you provide it explicitly with SetDecryptCert. A PFX (PKCS#12) bundles the certificate and its private key in one password-protected file. If the certificate and key are in separate files, use SetDecryptCert2 instead.

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 SetDecryptCert method, which explicitly provides a certificate (with
    //  access to its private key) for decrypting a received encrypted email.  Set the cert
    //  before loading the encrypted email so it can be decrypted automatically.

    CkEmail email = new CkEmail();

    //  Load a certificate together with its private key from a PFX file.
    CkCert cert = new CkCert();
    success = cert.LoadPfxFile("qa_data/certs/decryption.pfx","pfx_password");
    if (success == false) {
        Log.i(TAG, cert.lastErrorText());
        return true;
        }

    //  Provide the certificate to use for decryption.
    success = email.SetDecryptCert(cert);
    if (success == false) {
        Log.i(TAG, email.lastErrorText());
        return true;
        }

    //  Load the encrypted email; Chilkat decrypts it using the certificate.
    success = email.LoadEml("qa_data/eml/encrypted.eml");
    if (success == false) {
        Log.i(TAG, email.lastErrorText());
        return true;
        }

    Log.i(TAG, "ReceivedEncrypted = " + String.valueOf(email.get_ReceivedEncrypted()));
    Log.i(TAG, "Decrypted = " + String.valueOf(email.get_Decrypted()));

    //  Note: Paths such as "qa_data/..." are relative local filesystem paths,
    //  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."
  }
}