Android™
Android™
Set the Decryption Certificate and Private Key
See more POP3 Examples
Demonstrates the Chilkat MailMan.SetDecryptCert2 method, which explicitly specifies both the certificate and its associated private key, as separate objects, to use when decrypting S/MIME encrypted email. This example loads a .cer certificate and a PEM private key, registers both, and fetches a message Chilkat can decrypt.
Background: Credentials are not always packaged together. When the certificate is a standalone
.cer and the private key is a separate PEM (or other) file, SetDecryptCert2 takes the two objects individually. It is the two-object counterpart to SetDecryptCert, which expects a single certificate that already carries its private key (such as one loaded from a PFX).Chilkat Android™ Downloads
// 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.SetDecryptCert2 method, which explicitly specifies both the
// certificate and its associated private key (as separate objects) to use when decrypting
// S/MIME encrypted email.
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");
// Load the certificate (public) and the matching private key from separate files.
CkCert cert = new CkCert();
success = cert.LoadFromFile("qa_data/certs/recipient.cer");
if (success == false) {
Log.i(TAG, cert.lastErrorText());
return;
}
CkPrivateKey privKey = new CkPrivateKey();
success = privKey.LoadPemFile("qa_data/certs/recipient_privkey.pem");
if (success == false) {
Log.i(TAG, privKey.lastErrorText());
return;
}
// Specify the certificate and private key to use when decrypting downloaded email.
success = mailman.SetDecryptCert2(cert,privKey);
if (success == false) {
Log.i(TAG, mailman.lastErrorText());
return;
}
// Fetch a message; if it is encrypted, Chilkat decrypts it using the certificate and key.
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: Paths such as "qa_data/..." are relative local filesystem paths,
// 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."
}
}