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

Read email headers and display the body as text

See more Email Object Examples

Read the sender, recipients, subject, date, and an optional custom header from an EML file. Display the plain-text body when available, or convert HTML to text. Handle messages that contain neither body format.

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;
        }

    System.out.println("From: " + email.ck_from());
    System.out.println("Subject: " + email.subject());
    System.out.println("Date: " + email.emailDateStr());
    int i = 0;
    while ((i < email.get_NumTo())) {
        System.out.println("To: " + email.getToName(i) + "  " + email.getToAddr(i));
        i = i+1;
        }

    String referenceId = email.getHeaderField("X-Reference-ID");
    if (email.get_LastMethodSuccess() == true) {
        System.out.println("Reference: " + referenceId);
        }

    if (email.HasPlainTextBody() == true) {
        System.out.println(email.getPlainTextBody());
        }
    else {
        if (email.HasHtmlBody() == true) {
            CkHtmlToText reader = new CkHtmlToText();
            System.out.println(reader.toText(email.getHtmlBody()));
            }
        else {
            System.out.println("No text or HTML body.");
            }

        }
  }
}