• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown

Access & Read Embedded Email Attachments From Existing Email Message

Ranch Hand
Posts: 714
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This technical tip shows how to Read Embedded Email Attachments from Message. Sometimes, we get emails that have other emails embedded in them as an attachment. These embedded emails are complete messages having their own Recipients list, Subject, Body and Attachments. Furthermore, each of these messages can have embedded messages in them.
Using Aspose.Email Java API, the developers can access each embedded message as an individual message. We will elaborate such example using recursive functionality.

Steps to Access Embedded Messages from an existing Email Message

Please perform the following sequence of steps:

  • Create an instance of MailMessage class

  • Load the existing Email Message using the load() method exposed by MailMessage class and by specifying the correct MessageFormat.

  • Call Recursive method by passing the instance of MailMessage class as parameter.

    1. Iterate over the attachment collection of MailMessage Instance.

    2. Replace the invalid chars from Attachment name and restrict the name to 50 chars.

    3. Save the attachment to disk using save() method exposed by Attachment class.

    4. Create an instance of MailMessage class

    5. Load the existing Email Message using the load() method exposed by MailMessage class and by specifying the correct MessageFormat.

    6. Call Recursive method by passing the instance of MailMessage class as parameter.

    7. Repeat 1 – 6.

Sample Code for Reading Embedded Email Attachments using Aspose.Email for Java 

// Base folder to load and save files used in this demo

private static String strBaseFolder = "D:\\Data\\Aspose\\resources\\";

public static void main(String[] args)




        System.out.print("Reading message with embedded messages....");

        MailMessage message = MailMessage.load(strBaseFolder + "embedded.msg", MessageFormat.getMsg());




    catch (Exception ex)





private static void ParseMessage(MailMessage message)


    System.out.println("Subject: " + message.getSubject());

    System.out.println("Extracting attachments....");

    for (int i = 0; i < message.getAttachments().size(); i++)


        Attachment att = (Attachment) message.getAttachments().get(i);

        System.out.println("Attachment Name: " + att.getName());

        // Get the name of attachment. If msg subject contains characters like :, /, \ etc., replace with space

        // because windows cannot save files with these characters

        // also save first 50 characters as file name to avoid long file names

        String attFileName = att.getName().replace(".eml", "").replace(":", " ").replace("\\", " ").replace("/", " ").replace("?", "");

        if (attFileName.length() > 50)


            attFileName = attFileName.substring(0, 50);


        String attExt = (att.getName().substring(att.getName().lastIndexOf("."), att.getName().lastIndexOf(".") + 4));

        // Save the attachment to disk

        att.save(strBaseFolder + attFileName + attExt);

        // Check if it is an orphaned text attachment file (ATT00001.txt....) and of type eml

        if ((attExt.equals(".eml")) || (att.getContentType().getMediaType().equals("text/plain") && att.getName().contains(".txt") == true && att.getName().contains("ATT") == true))


            // Try to load this text file in MailMessage

            MailMessage attMsg = MailMessage.load(strBaseFolder + attFileName + attExt, MessageFormat.getEml());

            // Call the function recursively to parse this message and attachments





Overview: Aspose.Email for Java

 Aspose.Email for Java is a Non-Graphical Java component that enables Java applications to read and write MS Outlook MSG files from within a Java application without using MS Outlook. It enables developers to create new MSG file from scratch, update an existing MSG file, read Outlook MSG file & get its properties like subject, body, recipients in to, cc and bcc, adding or removing attachment, sender information & MAPI properties.  Aspose.Email can be used with Web as well as Desktop Application.

More about Aspose.Email for Java

Contact Information

Suite 119, 272 Victoria Avenue

Chatswood, NSW, 2067


Aspose - Your File Format Experts


Phone: 888.277.6734

Fax: 866.810.9465

Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic