Java Requires Chilkat v11.0.0+
Java
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 Java Downloads
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Email;
import com.chilkatsoft.chilkat2.HtmlToText;
public class ChilkatExample {
public static void main(String[] args) {
// Assumes Chilkat is unlocked. See Global Unlock Sample.
Email email = new Email();
try {
email.loadEml("with-attachments.eml");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
System.out.println("From: " + email.getFrom());
System.out.println("Subject: " + email.getSubject());
System.out.println("Date: " + email.getEmailDateStr());
int i = 0;
while (i < email.getNumTo()) {
System.out.println("To: " + email.getToName(i) + " " + email.getToAddr(i));
i = i + 1;
}
String referenceId = email.getHeaderField("X-Reference-ID");
if (referenceId != null) {
System.out.println("Reference: " + referenceId);
}
if (email.hasPlainTextBody()) {
System.out.println(email.getPlainTextBody());
} else {
if (email.hasHtmlBody()) {
HtmlToText reader = new HtmlToText();
System.out.println(reader.toText(email.getHtmlBody()));
} else {
System.out.println("No text or HTML body.");
}
}
}
}