Sample code for 30+ languages & platforms
Android™

Add a Custom Header Field to an Email

See more Email Object Examples

Demonstrates the Chilkat Email.AddHeaderField method, which adds a standard or custom header field. If the field already exists, this method replaces it (use AddHeaderField2 to allow duplicates). Header fields whose names begin with CKX- are not transmitted when the email is sent, but are preserved across XML save/load, making them handy for persistent metadata. This example adds a custom X- header and reads it back.

Background: Beyond the well-known headers (From, Subject, etc.), MIME lets you add arbitrary fields. By convention custom, non-standard fields are prefixed with X-, so mail systems know not to expect them in the standards. Applications use these to carry tracking IDs, campaign tags, or routing hints. Chilkat's CKX- convention goes a step further — those fields live with the object but are stripped before the message is actually sent.

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 AddHeaderField method, which adds a standard or custom header field.
    //  If the header field already exists, this method REPLACES it.  (To allow duplicates,
    //  use AddHeaderField2 instead.)  Header fields whose names begin with "CKX-" are not
    //  transmitted when the email is sent, but are preserved when saved to/loaded from XML.

    CkEmail email = new CkEmail();
    email.put_Subject("Custom header example");
    email.put_From("alice@example.com");

    //  Add a custom header field.
    email.AddHeaderField("X-Custom-Header","custom value");

    Log.i(TAG, "X-Custom-Header = " + email.getHeaderField("X-Custom-Header"));

  }

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