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

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

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Email;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.Mime;

public class ChilkatExample {

    public static void main(String[] args) {
        // 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";

        Mime mime = new Mime();

        try {
            mime.loadMimeFile(emlPath);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("---- MIME structure ----");
        System.out.println(mime.getStructure("text"));
        System.out.println("------------------------");

        Email email = new Email();
        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.getReceivedSigned());
        System.out.println("Email was Encrypted: " + email.getReceivedEncrypted());
        if (email.getReceivedSigned()) {
            System.out.println("Signature(s) valid = " + email.getSignaturesValid());
        }

        if (email.getReceivedEncrypted()) {
            System.out.println("Decrypted successfully = " + email.getDecrypted());
        }

        int i = 0;
        int numAttach = email.getNumAttachments();
        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.getNumRelatedItems();
        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.
        Email em = new Email();
        int numAttachedMessages = email.getNumAttachedMessages();
        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.getFrom());
            System.out.println("subject: " + em.getSubject());
            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.getNumReports();
        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()) {
            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"));

            JsonObject jsonDsnInfo = new JsonObject();
            email.getDsnInfo(jsonDsnInfo);
            jsonDsnInfo.setEmitCompact(false);
            System.out.println(jsonDsnInfo.emit());
        }
    }
}