Sample code for 30+ languages & platforms
Android™ Requires Chilkat v11.0.0+

Create an HTML preview with embedded images

See more Email Object Examples

Make an independent copy of with-attachments.eml, remove ordinary file attachments from the copy, and export its HTML with related images as data URLs. The original retains its attachments. Use a sanitized viewer when showing untrusted HTML.

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;

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.

    CkEmail email = new CkEmail();
    success = email.LoadEml("with-attachments.eml");
    if (success != true) {
        Log.i(TAG, email.lastErrorText());
        return;
        }

    CkEmail preview = new CkEmail();
    success = email.MakeCopy(preview);
    if (success != true) {
        Log.i(TAG, email.lastErrorText());
        return;
        }

    //  Keep related items: the HTML still refers to them.
    preview.DropAttachments();
    Log.i(TAG, "Files in original: " + String.valueOf(email.get_NumAttachments()) + "  Files in preview: " + String.valueOf(preview.get_NumAttachments()));
    if (preview.HasHtmlBody() != true) {
        Log.i(TAG, "This message has no HTML preview.");
        return;
        }

    CkStringBuilder html = new CkStringBuilder();
    success = preview.GetHtmlBodySb(true,html);
    if (success != true) {
        Log.i(TAG, preview.lastErrorText());
        return;
        }

    success = html.WriteFile("preview.html","utf-8",false);
    if (success != true) {
        Log.i(TAG, "Failed: html.WriteFile.");
        return;
        }

    Log.i(TAG, "Saved preview.html.");

  }

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