Sample code for 30+ languages & platforms
Android™

Get Each Alternative Body of an Email

See more Email Object Examples

Demonstrates the Chilkat Email.GetAlternativeBody method, which returns the Nth alternative body. The NumAlternatives property gives the number of alternatives, and indexes are zero-based. This example builds a message with plain-text and HTML alternatives and prints each one.

Background: A multipart/alternative email carries the same content in several forms — commonly plain text and HTML. Enumerating them by index lets you pull out exactly the representation you need: showing the HTML in a rich viewer, extracting the plain text for search or indexing, or comparing the two. Pair this with GetAlternativeContentType to learn what each indexed body actually is.

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);

    //  Demonstrates the GetAlternativeBody method, which returns the Nth alternative body.
    //  The NumAlternatives property gives the number of alternatives; indexes are zero-based.

    CkEmail email = new CkEmail();

    //  Create an email with plain-text and HTML alternatives.
    email.SetTextBody("This is the plain-text alternative.","text/plain");
    email.AddHtmlAlternativeBody("<html><body>This is the HTML alternative.</body></html>");

    int n = email.get_NumAlternatives();
    int i;
    for (i = 0; i <= n - 1; i++) {
        Log.i(TAG, "---- Alternative " + String.valueOf(i) + " ----");
        Log.i(TAG, email.getAlternativeBody(i));
        }


  }

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