Sample code for 30+ languages & platforms
Android™

Create a multipart/alternative MIME Entity

See more MIME Examples

Demonstrates the Chilkat Mime.NewMultipartAlternative method, which initializes the object as an empty multipart/alternative entity. It takes no arguments; the alternative representations are then appended.

Background: multipart/alternative holds several renderings of the same content and lets the client pick the best one it can display — the standard way to send an email as both plain text and HTML. Order matters: parts go from least to most preferred, so the plain-text version comes first and the HTML last, and a client shows the last one it understands.

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;

    //  Demonstrates the Mime.NewMultipartAlternative method, which initializes the object as an empty
    //  multipart/alternative entity, clearing any existing content.  It takes no arguments.

    CkMime mime = new CkMime();

    success = mime.NewMultipartAlternative();
    if (success == false) {
        Log.i(TAG, mime.lastErrorText());
        return;
        }

    //  multipart/alternative holds two representations of the SAME content; the client displays the
    //  last one it can render.  Add a plain-text part, then an HTML part.
    CkMime textPart = new CkMime();
    success = textPart.SetBodyFromPlainText("Hello (plain text version).");
    if (success == false) {
        Log.i(TAG, textPart.lastErrorText());
        return;
        }

    success = mime.AppendPart(textPart);
    if (success == false) {
        Log.i(TAG, mime.lastErrorText());
        return;
        }

    CkMime htmlPart = new CkMime();
    success = htmlPart.SetBodyFromHtml("<html><body><b>Hello</b> (HTML version).</body></html>");
    if (success == false) {
        Log.i(TAG, htmlPart.lastErrorText());
        return;
        }

    success = mime.AppendPart(htmlPart);
    if (success == false) {
        Log.i(TAG, mime.lastErrorText());
        return;
        }

    Log.i(TAG, "Number of alternatives: " + String.valueOf(mime.get_NumParts()));

  }

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