Sample code for 30+ languages & platforms
Android™

Add an HTML Alternative Body to an Email

See more Email Object Examples

Demonstrates the Chilkat Email.AddHtmlAlternativeBody method, which adds an HTML alternative body. Combined with a plain-text body, it produces a multipart/alternative email that offers both representations of the same content. This example sets a plain-text body, adds an HTML alternative, and prints the resulting alternative count.

Background: A well-formed HTML email usually ships two versions of the body: a rich HTML version and a plain-text fallback, wrapped in a multipart/alternative container. The receiving client displays whichever it prefers — modern clients show the HTML, while text-only clients (or spam filters and accessibility tools) use the plain text. Providing both improves deliverability and ensures every recipient can read the message.

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 AddHtmlAlternativeBody method, which adds an HTML alternative body to
    //  the email.  Combined with a plain-text body, this produces a multipart/alternative
    //  email that offers both representations.

    CkEmail email = new CkEmail();
    email.put_Subject("Plain-text and HTML alternatives");

    //  Start with a plain-text body.
    email.SetTextBody("This is the plain-text version of the message.","text/plain");

    //  Add an HTML alternative, creating a multipart/alternative email.
    email.AddHtmlAlternativeBody("<html><body><b>This is the HTML version.</b></body></html>");

    Log.i(TAG, "NumAlternatives = " + String.valueOf(email.get_NumAlternatives()));

  }

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