wood burning stoves 2.0*
The moose likes Blatant Advertising and the fly likes Access & Read Embedded Email Attachments From Existing Email Message Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Other » Blatant Advertising
Bookmark "Access & Read Embedded Email Attachments From Existing Email Message" Watch "Access & Read Embedded Email Attachments From Existing Email Message" New topic
Author

Access & Read Embedded Email Attachments From Existing Email Message

sherazam khan
Ranch Hand

Joined: Mar 10, 2010
Posts: 279
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 
[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);

        }

    }


}


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

Australia

Aspose - Your File Format Experts

sales@aspose.com


Phone: 888.277.6734

Fax: 866.810.9465

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Access & Read Embedded Email Attachments From Existing Email Message
 
Similar Threads
How to Read Barcode from Multi-page TIFF Images
Cleared SCJP with 100%!!!! YAHOO!!:-)
Sample Mobile Device App. to Generate & Display Barcode Image
Insert PDF Bookmarks While Converting a Spreadsheet to PDF file
link problem