Sample code for 30+ languages & platforms
Android™

Specify a Certificate for Encrypted Email

See more Email Object Examples

Demonstrates the Chilkat Email.AddEncryptCert method, which explicitly specifies a certificate to use when sending encrypted email. Call it once per recipient certificate; ClearEncryptCerts clears the list. This example loads a recipient certificate, registers it, and enables encrypted sending.

Background: When encrypting to multiple people, each recipient needs to be able to decrypt with their own private key. S/MIME handles this by encrypting the message's one-time content key separately under each recipient's public certificate and including all of those wrapped keys in the message. AddEncryptCert is how you build that recipient list — one call per certificate — giving explicit control over exactly whose certificates are used rather than relying on automatic lookup.

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 AddEncryptCert method, which explicitly specifies a certificate for
    //  sending encrypted email.  Call it once per recipient certificate.  Use ClearEncryptCerts
    //  to clear the list.

    CkEmail email = new CkEmail();
    email.put_Subject("Encrypted email");
    email.put_Body("Encrypted to the specified recipient certificate(s).");
    email.put_From("alice@example.com");
    email.AddTo("Bob","bob@example.com");

    //  Load a recipient certificate (public key) and add it to the encryption cert list.
    CkCert cert = new CkCert();
    success = cert.LoadFromFile("qa_data/certs/recipient.cer");
    if (success == false) {
        Log.i(TAG, cert.lastErrorText());
        return true;
        }

    success = email.AddEncryptCert(cert);
    if (success == false) {
        Log.i(TAG, email.lastErrorText());
        return true;
        }

    //  Request encrypted sending.
    email.put_SendEncrypted(true);

    Log.i(TAG, "Added the recipient's encryption certificate.");

    //  Note: The path "qa_data/certs/recipient.cer" 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."
  }
}