Sample code for 30+ languages & platforms
Android™

Enumerate the Header Fields of an Email

See more Email Object Examples

Demonstrates the read-only Chilkat Email.NumHeaderFields property together with GetHeaderFieldName and GetHeaderFieldValue to enumerate every header field. Indexing is zero-based, so fields run from 0 to NumHeaderFields - 1. Repeated header fields (a field name that appears more than once) are counted separately. This example builds a small email and prints each header field.

Background: A header field is a single Name: value line at the top of a MIME message. The same field name can legitimately appear multiple times — for example, a message can carry several Received lines, one added by each mail server it passed through. That is why enumerating by index (rather than looking up by name) matters: it lets you see every occurrence in the order it appears.

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 read-only Email.NumHeaderFields property and enumerating each
    //  header field by zero-based index using GetHeaderFieldName and GetHeaderFieldValue.

    CkEmail email = new CkEmail();
    email.put_Subject("Header field enumeration");
    email.put_From("mary@example.com");
    email.AddTo("Joe","joe@example.com");

    int n = email.get_NumHeaderFields();
    Log.i(TAG, "NumHeaderFields = " + String.valueOf(n));

    int i;
    for (i = 0; i <= n - 1; i++) {
        Log.i(TAG, email.getHeaderFieldName(i) + ": " + email.getHeaderFieldValue(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."
  }
}