Sample code for 30+ languages & platforms
Android™

Remove All Attachments from an Email

See more Email Object Examples

Demonstrates the Chilkat Email.DropAttachments method, which removes all ordinary attachments from the in-memory message. Related items (inline images, style sheets) and attached message/rfc822 emails are not affected. This example adds two attachments, drops them, and prints the count before and after.

Background: Chilkat distinguishes three kinds of enclosed content: ordinary attachments (files meant to be saved), related items (resources that support the HTML display), and nested attached messages. DropAttachments targets only the first category — useful, for example, when forwarding or re-sending a message body without carrying its original file attachments along.

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 DropAttachments method, which removes all ordinary attachments from
    //  the email.  Related items and attached messages are not affected.

    CkEmail email = new CkEmail();
    email.put_Subject("Drop attachments example");

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

    //  Remove all attachments.
    email.DropAttachments();

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