Sample code for 30+ languages & platforms
Android™

Get a Header Field by Name

See more Email Object Examples

Demonstrates the Chilkat Email.GetHeaderField method, which returns the value of a header field by name. Header-field names are case-insensitive, so X-Priority and x-priority refer to the same field. This example reads several headers by name.

Background: Looking up a header by name is the quickest way to read a known field like Subject or a custom X- header. One caveat: some header names (such as Received) can legitimately appear more than once. When a field may repeat and you need every occurrence, enumerate by index with GetHeaderFieldName and GetHeaderFieldValue instead of looking up by name.

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 GetHeaderField method, which returns the value of a header field by
    //  name.  Header-field names are case-insensitive.

    CkEmail email = new CkEmail();
    email.put_Subject("Quarterly report");
    email.put_From("alice@example.com");
    email.AddHeaderField("X-Priority","1");

    //  Get header field values by name.
    Log.i(TAG, "Subject = " + email.getHeaderField("Subject"));
    Log.i(TAG, "From = " + email.getHeaderField("From"));
    Log.i(TAG, "X-Priority = " + email.getHeaderField("x-priority"));

  }

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