Android™
Android™
S/MIME Encrypt .eml without Sending
See more Email Object Examples
Demonstrates how to encrypt an email using the recipient's digital certificate. This example just encrypts, and does not send the email.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;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
CkEmail email = new CkEmail();
success = email.LoadEml("c:/temp/email/unencrypted.eml");
if (success == false) {
Log.i(TAG, email.lastErrorText());
return;
}
// The email content is encrypted using AES with a 256-bit key, operating in GCM mode, which provides authenticated encryption.
email.put_Pkcs7CryptAlg("aes-gcm");
email.put_Pkcs7KeyLength(256);
email.put_OaepPadding(true);
email.put_OaepHash("sha256");
email.put_OaepMgfHash("sha256");
CkCert cert = new CkCert();
success = cert.LoadFromFile("c/temps/cert/recipient.cer");
if (success == false) {
Log.i(TAG, cert.lastErrorText());
return;
}
email.put_SendEncrypted(true);
email.SetEncryptCert(cert);
CkStringBuilder sbSmime = new CkStringBuilder();
// The mailman object applies the encryption by rendering the email according to the instructions (property settings) provided in the email object.
// No email is sent.
CkMailMan mailman = new CkMailMan();
success = mailman.RenderToMimeSb(email,sbSmime);
if (success == false) {
Log.i(TAG, mailman.lastErrorText());
return;
}
success = sbSmime.WriteFile("c:/temp/encryptedEmail.eml","utf-8",false);
if (success == false) {
Log.i(TAG, mailman.lastErrorText());
return;
}
Log.i(TAG, "Success!");
}
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."
}
}