Java
Java
Load .eml and Examine the Structure, Attachments, and Related Items
See more Email Object Examples
Demonstrates how to load examine the MIME structure of a .eml, and also examine the attachment and related item filenames, attached messages, and multipart/report and DSN information.Chilkat Java Downloads
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;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
String emlPath = "C:/AAWorkarea/beatrix/roesner.eml";
CkMime mime = new CkMime();
success = mime.LoadMimeFile(emlPath);
if (success == false) {
System.out.println(mime.lastErrorText());
return;
}
System.out.println("---- MIME structure ----");
System.out.println(mime.getStructure("text"));
System.out.println("------------------------");
CkEmail email = new CkEmail();
success = email.LoadEml(emlPath);
// Was this a signed and/or encrypted email?
// If so, then loading the .eml automatically unwraps
// (i.e. verifies signatures and decrypts) and the resultant
// email is what existed prior to signing/encrypting.
System.out.println("Email was Signed: " + email.get_ReceivedSigned());
System.out.println("Email was Encrypted: " + email.get_ReceivedEncrypted());
if (email.get_ReceivedSigned() == true) {
System.out.println("Signature(s) valid = " + email.get_SignaturesValid());
}
if (email.get_ReceivedEncrypted() == true) {
System.out.println("Decrypted successfully = " + email.get_Decrypted());
}
int i = 0;
int numAttach = email.get_NumAttachments();
System.out.println("Number of attachments = " + numAttach);
while (i < numAttach) {
System.out.println("---- Attachment " + i);
// Examine the filename (if any)
System.out.println("filename: " + email.getAttachmentFilename(i));
// Examine the content-ID (if any)
System.out.println("Content-ID: " + email.getAttachmentContentID(i));
// Examine the content-type
System.out.println("Content-Type: " + email.getAttachmentContentType(i));
// Examine the content-disposition
System.out.println("Content-Disposition" + email.getAttachmentHeader(i,"content-disposition"));
// Examine the attachment size:
System.out.println("Size (in bytes) of the attachment: " + email.GetAttachmentSize(i));
i = i+1;
}
System.out.println("--");
// Now for the related items.
// Note: A MIME sub-part can potentially be both a related item AND an attachment.
// The typical case is when the item is contained under the multipart/related enclosure and
// the item also has a "Content-Disposition" header indicating "attachment".
// The location within multipart/related makes it a "related item", yet the Content-Disposition can also make it semantically an attachment.
// Related items and attachments are not necessarily mutually exclusive.
int numRelated = email.get_NumRelatedItems();
System.out.println("Number of related items = " + numRelated);
i = 0;
while (i < numRelated) {
System.out.println("---- Related Item " + i);
// Examine the filename (if any)
System.out.println("filename: " + email.getRelatedFilename(i));
// Examine the content-ID (if any)
System.out.println("Content-ID: " + email.getRelatedContentID(i));
// Examine the content-type
System.out.println("Content-Type: " + email.getRelatedContentType(i));
// Examine the content-location (if any)
System.out.println("Content-Location" + email.getRelatedContentLocation(i));
i = i+1;
}
// The email could also have attached messages.
// An attached message is another email that was attached to this email.
CkEmail em = new CkEmail();
int numAttachedMessages = email.get_NumAttachedMessages();
System.out.println("Number of attached messages = " + numAttachedMessages);
i = 0;
while (i < numAttachedMessages) {
System.out.println("---- Attached message " + i);
// Examine the attached email
email.GetAttachedEmail(i,em);
System.out.println("from: " + em.ck_from());
System.out.println("subject: " + em.subject());
i = i+1;
}
// An email could also be a multipart/report email.
// This is a DSN (Delivery Status Notification)
// The NumReports property indicates how many "reports" exist.
int numReports = email.get_NumReports();
System.out.println("Number of reports = " + numReports);
i = 0;
while (i < numReports) {
System.out.println("---- Report " + i);
// Get the raw report data...
System.out.println(email.getReport(i));
i = i+1;
}
// If the email is a multipart/report, then the information
// from the message/delivery-status part of the email can be retrieved:
if (email.IsMultipartReport() == true) {
System.out.println("--- Delivery Status Information:");
System.out.println("Status: " + email.getDeliveryStatusInfo("Status"));
System.out.println("Action: " + email.getDeliveryStatusInfo("Action"));
System.out.println("Reporting-MTA: " + email.getDeliveryStatusInfo("Reporting-MTA"));
CkJsonObject jsonDsnInfo = new CkJsonObject();
email.GetDsnInfo(jsonDsnInfo);
jsonDsnInfo.put_EmitCompact(false);
System.out.println(jsonDsnInfo.emit());
}
}
}