Sample code for 30+ languages & platforms
Android™

Get an Alternative Body by Content-Type

See more Email Object Examples

Demonstrates the Chilkat Email.GetAlternativeBodyByContentType method, which returns an alternative body selected by its Content-Type — such as text/plain, text/html, or text/xml — instead of by numeric index. This example retrieves both the plain-text and HTML alternatives by content type.

Background: Selecting by Content-Type is often more convenient than by index, because you usually know which representation you want (the HTML, say) but not its position in the alternative list. This mirrors how a mail client works — it looks up the best matching type to display — and avoids assumptions about ordering, which can vary between messages.

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 GetAlternativeBodyByContentType method, which returns an alternative
    //  body selected by its Content-Type (such as text/plain, text/html, or text/xml) rather
    //  than by index.

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

    //  Get the plain-text alternative by its content type.
    String plain = email.getAlternativeBodyByContentType("text/plain");
    Log.i(TAG, "text/plain body: " + plain);

    //  Get the HTML alternative by its content type.
    String htmlBody = email.getAlternativeBodyByContentType("text/html");
    Log.i(TAG, "text/html body: " + htmlBody);

  }

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