Sample code for 30+ languages & platforms
Android™

Get a Header Attribute of an Attachment

See more Email Object Examples

Demonstrates the Chilkat Email.GetAttachmentAttr method, which returns a header-field attribute value from a header field of the Nth attachment. The first argument is the zero-based attachment index, the second names a header field, and the third names an attribute within it. This example reads the filename attribute of the attachment's Content-Disposition header.

Background: A MIME header can have named parameters after its main value, such as Content-Type: text/plain; charset="utf-8"; name="notes.txt". Here charset and name are attributes. GetAttachmentAttr pulls out one named attribute from a chosen header of a specific attachment, sparing you from parsing the raw header string and dealing with quoting or ordering.

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 GetAttachmentAttr method, which returns a header-field attribute value
    //  from a header field of the Nth attachment.  The first argument is the zero-based attachment
    //  index, the second is the header field name, and the third is the attribute name.

    CkEmail email = new CkEmail();
    email.put_Subject("Attachment header attribute");

    email.AddStringAttachment("notes.txt","Some notes.");

    //  Get the "filename" attribute of the attachment's Content-Disposition header (index 0).
    String fname = email.getAttachmentAttr(0,"Content-Disposition","filename");
    Log.i(TAG, "Attachment filename attribute: " + fname);

  }

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