Sample code for 30+ languages & platforms
Android™

Remove a Single Attachment from an Email

See more Email Object Examples

Demonstrates the Chilkat Email.DropSingleAttachment method, which removes the attachment at the specified zero-based index. Valid indexes run from 0 through NumAttachments - 1. This example adds two attachments, removes the first, and prints the count before and after.

Background: Attachments are addressed by a zero-based index, so the first is 0, the second 1, and so on. DropSingleAttachment removes just one — use it to prune a specific file (say, an oversized or unwanted attachment) while keeping the rest. Note that after a removal the remaining attachments shift down, so if you are deleting several by index, iterate from the highest index downward to avoid skipping any.

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 DropSingleAttachment method, which removes the attachment at the
    //  specified zero-based index.

    CkEmail email = new CkEmail();
    email.put_Subject("Drop a single attachment");

    email.AddStringAttachment("a.txt","first attachment");
    email.AddStringAttachment("b.txt","second attachment");
    Log.i(TAG, "NumAttachments before = " + String.valueOf(email.get_NumAttachments()));

    //  Remove the attachment at index 0 (the first attachment).
    email.DropSingleAttachment(0);

    Log.i(TAG, "NumAttachments after = " + String.valueOf(email.get_NumAttachments()));

  }

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