Sample code for 30+ languages & platforms
Android™

Save an Email to a Chilkat XML File

See more Email Object Examples

Demonstrates the Chilkat Email.SaveXml method, which converts the email object to Chilkat's XML representation and saves it to a file. It can later be reloaded with LoadXml. This example builds a message and saves it as XML.

Background: This XML format is an alternative to the standard .eml (MIME) format for persisting an email. Note that .eml also preserves Chilkat's CKX- metadata headers (which are always removed before an email is sent), so it captures the same information. Because .eml is universal and interoperable, Chilkat recommends using SaveEml / LoadEml over the XML format — even within Chilkat-based software.

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 SaveXml method, which converts the email object to Chilkat's XML
    //  representation and saves it to a file.  It can later be reloaded with LoadXml.

    CkEmail email = new CkEmail();
    email.put_Subject("Save as XML");
    email.put_From("alice@example.com");
    email.AddTo("Bob","bob@example.com");
    email.put_Body("Hello!");

    //  Save the email to a Chilkat XML file.
    success = email.SaveXml("qa_output/email.xml");
    if (success == false) {
        Log.i(TAG, email.lastErrorText());
        return true;
        }

    Log.i(TAG, "Saved to email.xml.");

    //  Note: The path "qa_output/..." is a relative local filesystem path,
    //  relative to the current working directory of the running application.

  }

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