Sample code for 30+ languages & platforms
Android™

Remove a Header Field from an Email

See more Email Object Examples

Demonstrates the Chilkat Email.RemoveHeaderField method, which removes by name all occurrences of a header field. Header-field names are case-insensitive, and every repeated occurrence with the given name is removed. This example adds a custom header, then removes it.

Background: Editing a message sometimes means deleting a header outright — stripping a tracking header, removing an X- flag before forwarding, or clearing a stale field before re-sending. Because a field name can appear more than once (like Received), RemoveHeaderField removes all matching occurrences in one call rather than just the first.

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 RemoveHeaderField method, which removes by name all occurrences of a
    //  header field.  Header-field names are case-insensitive.

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

    email.AddHeaderField("X-Custom-Header","some value");

    //  Use HasHeaderMatching to test for the header's presence.  ("*" matches any value.)
    boolean hasBefore = email.HasHeaderMatching("X-Custom-Header","*",false);
    Log.i(TAG, "Has X-Custom-Header before: " + String.valueOf(hasBefore));

    //  Remove all occurrences of the header field.
    email.RemoveHeaderField("X-Custom-Header");

    boolean hasAfter = email.HasHeaderMatching("X-Custom-Header","*",false);
    Log.i(TAG, "Has X-Custom-Header after: " + String.valueOf(hasAfter));

  }

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