Sample code for 30+ languages & platforms
Android™

Set a Preferred Charset for an Email

See more Email Object Examples

Demonstrates the Chilkat Email.PreferredCharset property. It applies only when building an email with non-English characters where the charset has not been explicitly set. Chilkat normally auto-selects a default charset per language (Chinese gb2312, Japanese shift_JIS, Korean ks_c_5601-1987, Thai windows-874, others iso-8859-*), and this property lets you steer that choice — for example choosing iso-2022-jp for Japanese. It is a preference, not a forced conversion: if the preferred charset cannot represent the text, it is ignored. This example sets a preferred charset.

Background: A single language often has several legacy charsets — Japanese, for instance, can be encoded as shift_JIS, euc-jp, or iso-2022-jp. Some mail environments expect one particular encoding, so PreferredCharset lets you nudge Chilkat toward it. Contrast this with the Charset property, which forces a specific charset: PreferredCharset is only a hint that Chilkat honors when it fits.

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 Email.PreferredCharset property.  It only applies when building an
    //  email that contains non-English characters and no charset is explicitly set.  Chilkat
    //  will prefer this charset if it can represent the email's text; otherwise it is ignored.

    CkEmail email = new CkEmail();
    email.put_Subject("Preferred charset example");
    email.put_Body("Japanese text would go here.");

    //  Prefer iso-2022-jp instead of the default shift_JIS for Japanese text.
    email.put_PreferredCharset("iso-2022-jp");

    Log.i(TAG, "PreferredCharset = " + email.preferredCharset());

  }

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