Sample code for 30+ languages & platforms
Android™

Remove the Plain-Text Body from an Email

See more Email Object Examples

Demonstrates the Chilkat Email.RemovePlainTextAlternative method, which removes the plain-text body from the email if one exists. Other body representations, attachments, and related items remain unchanged. This example builds a message with both plain-text and HTML alternatives, then removes the plain text.

Background: This is the counterpart to RemoveHtmlAlternative. Removing the plain-text alternative leaves an HTML-only message. Note that dropping the plain-text fallback is generally discouraged for real mail — it hurts accessibility and can raise spam scores — but it is occasionally needed when a downstream system expects a single HTML representation.

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 RemovePlainTextAlternative method, which removes the plain-text body
    //  from the email (if one exists).  Other body representations, attachments, and related
    //  items remain.

    CkEmail email = new CkEmail();
    email.put_Subject("Remove plain-text alternative");

    //  Create an email with both plain-text and HTML alternatives.
    email.SetTextBody("This is the plain-text version.","text/plain");
    email.AddHtmlAlternativeBody("<html><body>This is the HTML version.</body></html>");
    Log.i(TAG, "NumAlternatives before = " + String.valueOf(email.get_NumAlternatives()));
    Log.i(TAG, "HasPlainTextBody before: " + String.valueOf(email.HasPlainTextBody()));

    //  Remove the plain-text body, leaving the HTML alternative.
    email.RemovePlainTextAlternative();
    Log.i(TAG, "NumAlternatives after = " + String.valueOf(email.get_NumAlternatives()));
    Log.i(TAG, "HasPlainTextBody after: " + String.valueOf(email.HasPlainTextBody()));

  }

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