Access & Read Embedded Email Attachments From Existing Email Message
(
Dec 23 2011 - 03:42:29 AM by
sherazam) - [
print blog
post]
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
[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
- Homepage of Aspose.Email for Java: http://www.aspose.com/categories/java-components/aspose.email-for-java/default.aspx
- Download Aspose.Email for Java: http://www.aspose.com/community/files/72/java-components/aspose.email-for-java/default.aspx
- More Technical Tips by Aspose.Email for Java: http://www.aspose.com/documentation/java-components/aspose.email-for-java/programmers-guide.html
Contact Information
Aspose Pty Ltd, Suite 163,
79 Longueville Road
Lane Cove, NSW, 2066
Australia
http://www.aspose.com/
sales@aspose.com
Phone: 888.277.6734
Fax: 866.810.9465