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

Read the plain-text and HTML bodies separately

See more Email Object Examples

When both alternatives exist, Body returns HTML. Use the explicit body accessors to choose text for an index or HTML for a viewer. This example reads alternatives.eml from the previous example.

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("alternatives.eml");
    if (success != true) {
        System.out.println(email.lastErrorText());
        return;
        }

    //  A message can have either representation, both, or neither.
    if (email.HasPlainTextBody() == true) {
        System.out.println("Plain text: " + email.getPlainTextBody());
        }

    if (email.HasHtmlBody() == true) {
        System.out.println("HTML source: " + email.getHtmlBody());
        }

    if ((email.HasPlainTextBody() == false) && (email.HasHtmlBody() == false)) {
        System.out.println("No text or HTML body. Inspect the MIME parts.");
        }
  }
}