Sample code for 30+ languages & platforms
Android™

Set the Signing Certificate and Private Key

See more Email Object Examples

Demonstrates the Chilkat Email.SetSigningCert2 method, which explicitly sets the certificate and its corresponding private key as separate objects for sending digitally signed email. This example loads a .cer certificate and a PEM private key, sets both, and enables signed sending.

Background: When your signing certificate and its private key are stored separately — a public .cer plus a PEM key file — rather than combined in a PFX, SetSigningCert2 accepts the two objects individually. It is the two-object counterpart to SetSigningCert, which takes a single certificate that already carries its private key.

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 SetSigningCert2 method, which explicitly sets the certificate and its
    //  corresponding private key (as separate objects) for sending digitally signed email.

    CkEmail email = new CkEmail();
    email.put_Subject("Signed email");
    email.put_Body("This message will be sent with a digital signature.");
    email.put_From("alice@example.com");
    email.AddTo("Bob","bob@example.com");

    //  Load the certificate (public) and the matching private key from separate files.
    CkCert cert = new CkCert();
    success = cert.LoadFromFile("qa_data/certs/signer.cer");
    if (success == false) {
        Log.i(TAG, cert.lastErrorText());
        return true;
        }

    CkPrivateKey privKey = new CkPrivateKey();
    success = privKey.LoadPemFile("qa_data/certs/signer_privkey.pem");
    if (success == false) {
        Log.i(TAG, privKey.lastErrorText());
        return true;
        }

    //  Set the certificate and private key used to create the signature.
    success = email.SetSigningCert2(cert,privKey);
    if (success == false) {
        Log.i(TAG, email.lastErrorText());
        return true;
        }

    email.put_SendSigned(true);

    Log.i(TAG, "Signing certificate and private key set.");

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