Sample code for 30+ languages & platforms
Android™ Requires Chilkat v11.0.0+

Read email headers and display the body as text

See more Email Object Examples

Read the sender, recipients, subject, date, and an optional custom header from an EML file. Display the plain-text body when available, or convert HTML to text. Handle messages that contain neither body format.

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;

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.

    CkEmail email = new CkEmail();
    success = email.LoadEml("with-attachments.eml");
    if (success != true) {
        Log.i(TAG, email.lastErrorText());
        return;
        }

    Log.i(TAG, "From: " + email.ck_from());
    Log.i(TAG, "Subject: " + email.subject());
    Log.i(TAG, "Date: " + email.emailDateStr());
    int i = 0;
    while ((i < email.get_NumTo())) {
        Log.i(TAG, "To: " + email.getToName(i) + "  " + email.getToAddr(i));
        i = i + 1;
        }

    String referenceId = email.getHeaderField("X-Reference-ID");
    if (email.get_LastMethodSuccess() == true) {
        Log.i(TAG, "Reference: " + referenceId);
        }

    if (email.HasPlainTextBody() == true) {
        Log.i(TAG, email.getPlainTextBody());
        }
    else {
        if (email.HasHtmlBody() == true) {
            CkHtmlToText reader = new CkHtmlToText();
            Log.i(TAG, reader.toText(email.getHtmlBody()));
            }
        else {
            Log.i(TAG, "No text or HTML body.");
            }

        }


  }

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