article

How to Manage Attachments in Existing & New Email Messages using Java inside Android Apps

Email
Submitted on: 12/23/2015 8:19:03 AM
By: Sherazam  
Level: Intermediate
User Rating: Unrated
Compatibility: Java (JDK 1.3), Java (JDK 1.4), Java (JDK 1.5)
Views: 4376
 
     This technical tip shows how to manage attachments in email message inside Android Apps. In some circumstances developers want to access and manipulate an email's attachments. Aspose.Email for Android provides a handful of collections and methods to extract attachments and work with them in other ways. Furthermore, using this API, it is possible to add and remove attachments at run time. To demonstrate these features, this article loads existing email messages from disk and accesses their attachment collection. This article explains how to add attachments to a new email message, extract attachments from existing emails and adding or removing attachments to and from existing emails.

 
				This technical tip shows how to manage attachments in email message inside Android Apps. In some circumstances developers want to access and manipulate an email's attachments. Aspose.Email for Android provides a handful of collections and methods to extract attachments and work with them in other ways. Furthermore, using this API, it is possible to add and remove attachments at run time. To demonstrate these features, this article loads existing email messages from disk and accesses their attachment collection. This article explains how to add attachments to a new email message, extract attachments from existing emails and adding or removing attachments to and from existing emails.
Adding Attachments to a New Email Message
public static void AddAttachmentsToMessage()
{
//Base folder
String strBaseFolder = Environment.getExternalStorageDirectory().getPath();
strBaseFolder = strBaseFolder + "/";
//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(strBaseFolder + "1.txt");
//Add attachment in instance of MailMessage class
message.getAttachments().add(attachment);
//Add 2nd Attachment
message.getAttachments().add(new Attachment(strBaseFolder + "1.jpg"));
//Add 3rd Attachment
message.getAttachments().add(new Attachment(strBaseFolder + "1.doc"));
//Add 4th Attachment
message.getAttachments().add(new Attachment(strBaseFolder + "1.rar"));
//Add 5th Attachment
message.getAttachments().add(new Attachment(strBaseFolder + "1.pdf"));
//Save message to disc
message.save(strBaseFolder + "output.msg",MessageFormat.getMsg());
}
Extracting Attachments from an Existing Email Message
public static void ExtractAttachmentsFromMessage()
{
// Base folder for reading and writing files
String strBaseFolder = Environment.getExternalStorageDirectory().getPath();
strBaseFolder = strBaseFolder + "/";
//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());
}
}
Adding or Removing Attachments from an Existing Email Message
public static void RemoveAttachmentsFromMessage()
{
// Base folder for reading and writing files
String strBaseFolder = Environment.getExternalStorageDirectory().getPath();
strBaseFolder = strBaseFolder + "/";
//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());
}
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.


Other 6 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.