Important alert: (current site time 7/16/2013 7:22:35 AM EDT)
 

VB icon

Access & Read Embedded Email Attachments From Existing Email Message

Email
Submitted on: 12/23/2011 11:52:38 AM
By: aspose_seo 
Level: Intermediate
User Rating: Unrated
Compatibility: Java (JDK 1.3), Java (JDK 1.4)
Views: 1051
 
     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.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: Access & Read Embedded Email Attachments From Existing Email Message
// Description: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. 
// By: aspose_seo
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=6925&lngWId=2//for details.//**************************************

// 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);
}
}
}


Other 11 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.