Sample code for 30+ languages & platforms
Android™

Provide a Certificate Vault to MailMan

See more POP3 Examples

Demonstrates the Chilkat MailMan.UseCertVault method, which provides an XML certificate vault to be searched for certificates and private keys when MailMan performs operations such as encryption, decryption, signing, or signature verification. This example builds a vault from a PFX, attaches it, and fetches a message that Chilkat can decrypt using the vault.

Background: A certificate vault is a portable, in-memory store of certificates and private keys. Rather than wiring up a specific certificate for each operation, you load your credentials into one XmlCertVault and hand it to MailMan, which then searches it automatically whenever a key is needed. This is especially useful on platforms without an OS certificate store, or when one application handles several identities.

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.UseCertVault method, which provides an XML certificate vault to
    //  be searched for certificates and private keys when MailMan performs operations such as
    //  encryption, decryption, signing, or signature verification.

    CkMailMan mailman = new CkMailMan();

    //  Configure the POP3 server connection.
    mailman.put_MailHost("pop.example.com");
    mailman.put_MailPort(995);
    mailman.put_PopSsl(true);
    mailman.put_PopUsername("user@example.com");
    mailman.put_PopPassword("myPassword");

    //  Build a certificate vault from a PFX (certificate + private key).
    CkXmlCertVault vault = new CkXmlCertVault();
    success = vault.AddPfxFile("qa_data/certs/certs.pfx","pfx_password");
    if (success == false) {
        Log.i(TAG, vault.lastErrorText());
        return;
        }

    //  Make the vault available to MailMan for crypto operations.
    success = mailman.UseCertVault(vault);
    if (success == false) {
        Log.i(TAG, mailman.lastErrorText());
        return;
        }

    //  Fetch a message; Chilkat draws on the vault if a private key is needed to decrypt it.
    CkEmail email = new CkEmail();
    success = mailman.FetchOne(false,0,1,email);
    if (success == false) {
        Log.i(TAG, mailman.lastErrorText());
        return;
        }

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

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

    //  Note: Explicitly connecting/authenticating is optional.  Chilkat MailMan automatically
    //  connects and authenticates -- using the property settings above -- whenever a server
    //  operation requires it.  Calling the explicit connect/authenticate methods can still be
    //  helpful to determine whether a failure occurs while connecting or while authenticating.

  }

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