Sample code for 30+ languages & platforms
Android™

Remove a Single Related Item from an Email

See more Email Object Examples

Demonstrates the Chilkat Email.DropRelatedItem method, which removes the related item at a given zero-based index. A related item is typically an embedded image or style sheet referenced from the HTML body (via a cid: link or Content-Location). This example adds a related style sheet, then removes it by index, printing the count before and after.

Background: Related items live in the message's multipart/related enclosure and are indexed from 0 to NumRelatedItems - 1. Removing one by index is useful when editing a received message — for example stripping a tracking pixel or an unwanted embedded image — while leaving the HTML body and any other related resources in place. To remove them all at once, use DropRelatedItems.

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 DropRelatedItem method, which removes the related item at a given
    //  zero-based index.  A related item is typically an embedded image or style sheet
    //  referenced from the HTML body.

    CkEmail email = new CkEmail();

    //  Set an HTML body that references a related style sheet by name (Content-Location).
    email.SetHtmlBody("<html><head><link rel=\"stylesheet\" href=\"styles.css\"/></head><body>Styled.</body></html>");

    //  Add the related item (becomes index 0).
    email.AddRelatedString2("styles.css","body { color: navy; }","utf-8");
    Log.i(TAG, "NumRelatedItems before = " + String.valueOf(email.get_NumRelatedItems()));

    //  Remove the related item at index 0.
    email.DropRelatedItem(0);

    Log.i(TAG, "NumRelatedItems after = " + String.valueOf(email.get_NumRelatedItems()));

  }

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