Sample code for 30+ languages & platforms
Android™

Save an Email to a .eml File

See more Email Object Examples

Demonstrates the Chilkat Email.SaveEml method, which converts the email object to EML and saves it to a file. An EML file contains the complete RFC822/MIME message — headers, bodies, related items, and attachments. This example builds a message and saves it.

Background: Saving as .eml writes the message in the universal MIME format that virtually every mail client can open, making it ideal for archiving, sharing, or handing a message to another program. It is the counterpart to LoadEml. A saved .eml also retains Chilkat's CKX- metadata headers (a way to store your own metadata with the email; they are always stripped before an email is sent), so .eml is the recommended way to persist a message even within Chilkat-based software. A separate Chilkat XML format (SaveXml) exists as an alternative, but offers no metadata advantage over .eml.

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 SaveEml method, which converts the email object to EML and saves it to
    //  a file.  An EML file contains the complete RFC822/MIME message, including headers,
    //  bodies, related items, and attachments.

    CkEmail email = new CkEmail();
    email.put_Subject("Save as EML");
    email.put_From("alice@example.com");
    email.AddTo("Bob","bob@example.com");
    email.put_Body("Hello, this message will be saved as a .eml file.");

    //  Save the email to a .eml file.
    success = email.SaveEml("qa_output/message.eml");
    if (success == false) {
        Log.i(TAG, email.lastErrorText());
        return true;
        }

    Log.i(TAG, "Saved to message.eml.");

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