Sample code for 30+ languages & platforms
Android™

Set the Charset of an Attachment

See more Email Object Examples

Demonstrates the Chilkat Email.SetAttachmentCharset method, which sets the charset parameter of the Content-Type header field for the attachment at a given zero-based index. This example adds a text attachment and sets its charset to utf-8.

Background: For a text attachment, the charset parameter tells the receiving client which character encoding the bytes use, so accented or non-Latin text renders correctly. Setting it explicitly (typically utf-8) removes ambiguity when the attachment contains non-ASCII content — without it, a client may guess wrong and display garbled characters.

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);

    boolean success = false;

    //  Demonstrates the SetAttachmentCharset method, which sets the charset parameter of the
    //  Content-Type header field for the attachment at the given zero-based index.

    CkEmail email = new CkEmail();
    email.put_Subject("Set attachment charset");

    email.AddStringAttachment("notes.txt","Some notes.");

    //  Set the charset of the first attachment (index 0) to utf-8.
    success = email.SetAttachmentCharset(0,"utf-8");
    if (success == false) {
        Log.i(TAG, email.lastErrorText());
        return true;
        }

    //  The attachment's Content-Type now includes charset="utf-8".
    Log.i(TAG, email.getMime());

  }

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