Sample code for 30+ languages & platforms
Android™

Get an Alternative Body into a BinData

See more Email Object Examples

Demonstrates the Chilkat Email.GetAlternativeBodyBd method, which copies the contents of the Nth alternative body into a BinData object. The first alternative body is at index 0, and it should only be called when NumAlternatives is greater than 0. This example builds a message with plain-text and HTML alternatives and copies the first into a BinData.

Background: This is the binary counterpart to GetAlternativeBody (which returns text). Reading an alternative into a BinData preserves the exact bytes — important when a body part uses a binary or unusual transfer encoding, or when you intend to hash it or write it out verbatim rather than decode it to a string.

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 GetAlternativeBodyBd method, which copies the contents of the Nth
    //  alternative body into a BinData object.  The first alternative body is at index 0; call
    //  only when NumAlternatives is greater than 0.

    CkEmail email = new CkEmail();
    email.put_Subject("GetAlternativeBodyBd example");

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

    //  Copy the first alternative body (index 0) into a BinData object.
    CkBinData bd = new CkBinData();

    success = email.GetAlternativeBodyBd(0,bd);
    if (success == false) {
        Log.i(TAG, email.lastErrorText());
        return true;
        }

    Log.i(TAG, "Alternative 0 size (bytes) = " + String.valueOf(bd.get_NumBytes()));

  }

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