Sample code for 30+ languages & platforms
Android™

Add a Custom Header to an Email Attachment

See more Email Object Examples

Demonstrates the Chilkat Email.AddAttachmentHeader method, which adds a custom MIME header field to an existing attachment identified by its zero-based index. This example adds an attachment (which becomes index 0), then attaches an extra header field to it and prints the resulting MIME.

Background: In MIME, every attachment is itself a mini-message with its own small block of headers — things like Content-Type, Content-Disposition, and Content-Transfer-Encoding that describe that specific part. AddAttachmentHeader lets you insert additional fields into that per-part header block, which is occasionally needed for interoperability with systems that look for custom X- headers or content identifiers on individual attachments.

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 AddAttachmentHeader method, which adds a custom MIME header field to
    //  an existing attachment, identified by its zero-based index.

    CkEmail email = new CkEmail();
    email.put_Subject("Attachment with a custom header");
    email.put_Body("The attachment has an extra MIME header.");

    //  Add an attachment; it becomes attachment index 0.
    email.AddStringAttachment("data.txt","Attachment body.");

    //  Add a custom header field to the first attachment (index 0).
    email.AddAttachmentHeader(0,"X-Custom-Attachment-Header","some value");

    //  The custom header now appears in the attachment's MIME part.
    Log.i(TAG, email.getMime());

  }

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