Android™
Android™
Set the Recipient Encryption Certificate
See more Email Object Examples
Demonstrates the Chilkat Email.SetEncryptCert method, which sets the explicit recipient encryption certificate for sending an encrypted email. This example loads the recipient's certificate, sets it, and enables encrypted sending.
Background: To encrypt a message for a recipient you need their public certificate — hence a
.cer file (no private key) suffices. SetEncryptCert specifies a single recipient certificate; to encrypt for multiple recipients, use AddEncryptCert once per certificate. Either way, setting SendEncrypted to true is what actually requests encryption when the message is sent.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 SetEncryptCert method, which sets the explicit recipient encryption
// certificate for sending an encrypted email.
CkEmail email = new CkEmail();
email.put_Subject("Encrypted email");
email.put_Body("This message will be sent S/MIME encrypted.");
email.put_From("alice@example.com");
email.AddTo("Bob","bob@example.com");
// Load the recipient's certificate (only the public key is needed to encrypt).
CkCert cert = new CkCert();
success = cert.LoadFromFile("qa_data/certs/recipient.cer");
if (success == false) {
Log.i(TAG, cert.lastErrorText());
return true;
}
// Set the explicit recipient encryption certificate.
success = email.SetEncryptCert(cert);
if (success == false) {
Log.i(TAG, email.lastErrorText());
return true;
}
// Request encrypted sending.
email.put_SendEncrypted(true);
Log.i(TAG, "Encryption certificate set.");
// 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."
}
}