Sample code for 30+ languages & platforms
Android™

Check if a Received Email Was Decrypted

See more Email Object Examples

Demonstrates the read-only Chilkat Email.Decrypted property, which is true if the email arrived encrypted and was successfully decrypted. This property is only meaningful when ReceivedEncrypted is true, so you should always check ReceivedEncrypted first — a false value can mean either that the message was not encrypted or that decryption did not succeed. This example loads a received email and reports its encryption/decryption status.

Background: Encrypted email typically uses S/MIME. The sender encrypts the message with the recipient's public-key certificate, and only the holder of the matching private key can decrypt it. When Chilkat loads such a message and has access to the private key, it decrypts automatically; ReceivedEncrypted tells you the message arrived encrypted, and Decrypted tells you whether decryption succeeded.

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 Email.Decrypted property.
    //  Decrypted is true if the email arrived encrypted and was successfully
    //  decrypted.  It is only meaningful when ReceivedEncrypted is true.

    CkEmail email = new CkEmail();

    //  Load an email that was received (from a .eml file).
    success = email.LoadEml("qa_data/eml/received.eml");
    if (success == false) {
        Log.i(TAG, email.lastErrorText());
        return true;
        }

    //  Always check ReceivedEncrypted first.
    if (email.get_ReceivedEncrypted() == true) {
        if (email.get_Decrypted() == true) {
            Log.i(TAG, "The email was encrypted and was successfully decrypted.");
            }
        else {
            Log.i(TAG, "The email was encrypted but could NOT be decrypted.");
            }

        }
    else {
        Log.i(TAG, "The email did not arrive encrypted.");
        }


  }

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