Sample code for 30+ languages & platforms
Android™

Get an Attached Message as an Email Object

See more Email Object Examples

Demonstrates the Chilkat Email.GetAttachedEmail method, which copies the Nth embedded message/rfc822 MIME part into another Email object. The index is zero-based (valid values run from 0 through NumAttachedMessages - 1). This example attaches an email and then extracts it back into its own object.

Background: When a message forwards another email as an attachment, that nested message is a complete email in its own right. GetAttachedEmail turns it back into a fully navigable Email object so you can read its subject, sender, body, and even its own attachments — essential for processing forwarded mail, or for tools that unpack reported spam/phishing to examine the original.

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 GetAttachedEmail method, which copies the Nth embedded message/rfc822
    //  MIME part into another Email object.  The index is zero-based.

    //  Build an inner email and attach it to an outer email.
    CkEmail innerEmail = new CkEmail();
    innerEmail.put_Subject("Embedded message");
    innerEmail.put_From("alice@example.com");
    innerEmail.put_Body("This is the embedded message.");

    CkEmail email = new CkEmail();
    email.put_Subject("Has an attached message");
    success = email.AttachEmail(innerEmail);
    if (success == false) {
        Log.i(TAG, email.lastErrorText());
        return true;
        }

    //  Copy the first embedded message (index 0) into its own Email object.
    CkEmail attached = new CkEmail();
    success = email.GetAttachedEmail(0,attached);
    if (success == false) {
        Log.i(TAG, email.lastErrorText());
        return true;
        }

    Log.i(TAG, "Attached email subject: " + attached.subject());
    Log.i(TAG, "Attached email from: " + attached.ck_from());

  }

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