Sample code for 30+ languages & platforms
Android™

Get an Attachment as Text with CRLF Line Endings

See more Email Object Examples

Demonstrates the Chilkat Email.GetAttachmentStringCrLf method, which retrieves an attachment's data as text with all end-of-line sequences translated to CRLF (\r\n). The first argument is the zero-based attachment index and the second is the charset used to interpret the bytes. This example reads a text attachment and normalizes its line endings to CRLF.

Background: Different systems mark the end of a line differently — Unix uses a single LF (\n), classic Mac used CR, and Windows/most internet protocols use CRLF (\r\n). Text pulled from an attachment can contain any of these. This method normalizes them all to CRLF, which is convenient when the text will be written to a protocol or format that expects consistent CRLF line endings, avoiding mixed-newline surprises.

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 GetAttachmentStringCrLf method, which retrieves an attachment's data as
    //  text with all end-of-line sequences translated to CRLF.  The first argument is the
    //  zero-based attachment index and the second is the charset used to interpret the bytes.

    CkEmail email = new CkEmail();
    email.put_Subject("Attachment as text (CRLF)");

    email.AddStringAttachment("notes.txt","Line one.\nLine two.\nLine three.");

    //  Get the first attachment (index 0) as text with CRLF line endings.
    String content = email.getAttachmentStringCrLf(0,"utf-8");
    Log.i(TAG, "Attachment 0 text (CRLF-normalized):");
    Log.i(TAG, content);

  }

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