Sample code for 30+ languages & platforms
CkJava 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 CkJava Downloads

CkJava
import com.chilkatsoft.*;

public class ChilkatExample {

  static {
    try {
        System.loadLibrary("chilkat");
    } catch (UnsatisfiedLinkError e) {
      System.err.println("Native code library failed to load.\n" + e);
      System.exit(1);
    }
  }

  public static void main(String argv[])
  {
    boolean success = false;

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

    CkEmail email = new CkEmail();
    success = email.LoadEml("with-attachments.eml");
    if (success != true) {
        System.out.println(email.lastErrorText());
        return;
        }

    CkEmail preview = new CkEmail();
    success = email.MakeCopy(preview);
    if (success != true) {
        System.out.println(email.lastErrorText());
        return;
        }

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

    CkStringBuilder html = new CkStringBuilder();
    success = preview.GetHtmlBodySb(true,html);
    if (success != true) {
        System.out.println(preview.lastErrorText());
        return;
        }

    success = html.WriteFile("preview.html","utf-8",false);
    if (success != true) {
        System.out.println("Failed: html.WriteFile.");
        return;
        }

    System.out.println("Saved preview.html.");
  }
}