Read Embedded Email Attachments from Message

Skip to end of metadata
Go to start of metadata
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 an 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.
Programming Sample
[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)
{

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

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

        System.out.println("Success");
    }
    catch (Exception ex)
    {
        System.out.println(ex.getMessage());
    }
}

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
            ParseMessage(attMsg);
        }
    }
}
 
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.