Sample code for 30+ languages & platforms
Android™

Add a String Attachment with a Specified Charset

See more Email Object Examples

Demonstrates the Chilkat Email.AddStringAttachment2 method, which adds a text attachment directly from an in-memory string and encodes it using a specified charset. The first argument is the attachment filename placed in the MIME (it is not a path to an existing file), the second is the text content, and the third is the charset used to encode the string. This example attaches a UTF-8 encoded text file.

Background: This is the charset-aware version of AddStringAttachment. Because text must be converted to bytes before it travels in a MIME part, the charset determines how non-ASCII characters (accents, non-Latin scripts) are represented. utf-8 is the safe modern default that can encode any character; the third argument may also begin with bom- or no-bom- to control whether a byte-order mark is written.

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 AddStringAttachment2 method, which adds a text attachment from an
    //  in-memory string and encodes it using a specified charset.  The first argument is the
    //  attachment filename (not a path to read), the second is the content, the third is the charset.

    CkEmail email = new CkEmail();
    email.put_Subject("Email with a charset-encoded string attachment");
    email.put_Body("See the attached text file.");

    //  Add a string attachment named "notes.txt", encoding the content as utf-8.
    email.AddStringAttachment2("notes.txt","Some notes with accented text.","utf-8");

    Log.i(TAG, "NumAttachments = " + String.valueOf(email.get_NumAttachments()));

  }

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