Sample code for 30+ languages & platforms
Android™

Count the Attachments in an Email

See more Email Object Examples

Demonstrates the read-only Chilkat Email.NumAttachments property, which is the number of ordinary attachments in the email. Attachment indexes are zero-based. Related resources inside a multipart/related enclosure are counted separately by NumRelatedItems, and embedded message/rfc822 emails by NumAttachedMessages. This example adds two attachments and prints the count.

Background: An "attachment" is a MIME part meant to be saved or opened as a separate file, as opposed to being displayed as the message body. Chilkat decides whether a part is an attachment by weighing its MIME structure, content type, and Content-Disposition together — so a part can count as an attachment even without an explicit Content-Disposition: attachment header. Note that if an email was downloaded from an IMAP server without its attachment data, this property reflects only what is actually present in the object.

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 read-only Email.NumAttachments property, which is the number
    //  of ordinary attachments contained in the email.  Attachment indexes are zero-based.

    CkEmail email = new CkEmail();
    email.put_Subject("Email with attachments");
    email.put_Body("Two files are attached.");

    //  Add two string attachments.
    email.AddStringAttachment("readme.txt","This is the first attachment.");
    email.AddStringAttachment("notes.txt","This is the second attachment.");

    Log.i(TAG, "NumAttachments = " + 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."
  }
}