Sample code for 30+ languages & platforms
Android™

Get the Name of the Nth Header Field

See more Email Object Examples

Demonstrates the Chilkat Email.GetHeaderFieldName method, which returns the name of the Nth header field. The NumHeaderFields property gives the count, and indexing is zero-based. This example enumerates the names of all header fields.

Background: Enumerating headers by index — rather than looking them up by name — lets you discover which fields a message actually contains and see repeated fields (like multiple Received lines) in order. Pair GetHeaderFieldName with GetHeaderFieldValue at the same index to walk the entire header block as name/value pairs.

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 GetHeaderFieldName method, which returns the name of the Nth header
    //  field.  The NumHeaderFields property gives the number of header fields; indexing is
    //  zero-based.

    CkEmail email = new CkEmail();
    email.put_Subject("Enumerate header names");
    email.put_From("alice@example.com");
    email.AddTo("Bob","bob@example.com");

    int n = email.get_NumHeaderFields();
    int i;
    for (i = 0; i <= n - 1; i++) {
        Log.i(TAG, "Header " + String.valueOf(i) + " name: " + email.getHeaderFieldName(i));
        }


  }

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