Important alert: (current site time 7/15/2013 4:10:30 AM EDT)
 

article

Manage Attachments in Existing & New Email Messages inside Java Apps

Email
Submitted on: 7/10/2013 3:20:50 PM
By: aspose_seo 
Level: Intermediate
User Rating: Unrated
Compatibility: Java (JDK 1.3), Java (JDK 1.4), Java (JDK 1.5)
Views: 269
 
     This technical tip shows how to manage attachments in email message. There can be certain circumstances when the developers want to access and manipulate the Attachments of an Email Message. Aspose.Email Java API provides the handful of collections and methods to perform a task like Extraction of Attachments. Furthermore, using this API one can Add or Remove Attachments at run time. To demonstrate these features, we will load existing Email Messages from disk and access their Attachment Collection.

 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article 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 article (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 article 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 article or article's description.
				This technical tip shows how to manage attachments in email message. There can be certain circumstances when the developers want to access and manipulate the Attachments of an Email Message. Aspose.Email Java API provides the handful of collections and methods to perform a task like Extraction of Attachments. Furthermore, using this API one can Add or Remove Attachments at run time. To demonstrate these features, we will load existing Email Messages from disk and access their Attachment Collection. 
Steps to Extract Attachments from an existing Email Message
Please perform the following sequence of steps to save the Attachments from existing Messages:* 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.* Create an instance of AttachmentCollection class and fill it with Attachments from the instance of MailMessage using getAttachments() method.* Iterate over the AttachmentCollection.# Create an instance of Attachment class and fill it with indexed value from AttachmentCollection using get() method.# Save the attachment to disk using the save() method exposed by Attachment class.
Adding Attachments to a New Email Message
//Create an instance of MailMessage class
MailMessage message = new MailMessage();
//From
message.setFrom(new MailAddress("sender@sender.com"));
//to whom
message.getTo().add(new MailAddress("receiver@gmail.com"));
//Adding 1st attachment
//Create an instance of Attachment class
Attachment attachment;
//Load an attachment
attachment = new Attachment("1.txt");
//Add attachment in instance of MailMessage class
message.getAttachments().add(attachment);
//Add 2nd Attachment
message.getAttachments().add(new Attachment("1.jpg"));
//Add 3rd Attachment
message.getAttachments().add(new Attachment("1.doc"));
//Add 4th Attachment
message.getAttachments().add(new Attachment("1.rar"));
//Add 5th Attachment
message.getAttachments().add(new Attachment("1.pdf"));
//Save message to disc
message.save("output.msg",MessageFormat.getMsg());
Extract Attachments from an existing Email Message
public static void main(String[] args)
{
// Base folder for reading and writing files
String strBaseFolder = "D:\\Data\\Aspose\\resources\\";
//Initialize and Load an existing EML file by specifying the MessageFormat
MailMessage msg = MailMessage.load(strBaseFolder + "AnEmail.eml", MessageFormat.getEml());
//Initialize AttachmentCollection object with MailMessage Attachments
AttachmentCollection attachments = msg.getAttachments();
//Iterate over the AttachmentCollection
for(int index = 0; index < attachments.size(); index++)
{
//Initialize Attachment object and Get the indexed Attachment reference
Attachment attachment = (Attachment) attachments.get(index);
//Display Attachment Name
System.out.println(attachment.getName());
//Save Attachment to disk
attachment.save(strBaseFolder + "attachment_"+ attachment.getName());
}
}
Add or Remove Attachments from an existing Email Message
public static void main(String[] args)
{
// Base folder for reading and writing files
String strBaseFolder = "D:\\Data\\Aspose\\resources\\";
//Initialize and Load an existing EML file by specifying the MessageFormat
MailMessage message = MailMessage.load(strBaseFolder + "AnEmail.eml", MessageFormat.getEml());
//Initialize AttachmentCollection object with MailMessage Attachments
AttachmentCollection attachments = message.getAttachments();
System.out.println("Attachment Count: " + attachments.size());
//Check if AttachmentCollection size is greater than 0
if(attachments.size() > 0)
{
//Remove Attachment from index location 0
attachments.remove(0);
System.out.println("Attachment Count: " + attachments.size());
}
//Add a PDF file as Attachment to the message
message.addAttachment(new Attachment(strBaseFolder + "Blank.PDF"));
System.out.println("Attachment Count: " + attachments.size());
//Save the Email message to disk by specifying the EML MailMessageSaveType
message.save(strBaseFolder + "message.eml", MailMessageSaveType.getEmlFormat());
}

More about Aspose.Email for Java


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 article (in the Intermediate category)?
(The article 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 article, please click here instead.)
 

To post feedback, first please login.