Load Existing Email Message and Modify its Contents inside Android Apps


/ Published in: Java
Save to your folder(s)

This technical tip shows how to load any existing email message and modify its contents before saving it back to disk using Aspose.Email for Android API. To do this successfully, specify the MessageFormat when loading the email message from disk. In addition, it is important to specify the correct MailMessageSaveType when saving the message back to disk.

To update and save an email, the following steps can be used:
1. Create an instance of the MailMessage class.
2. Load an existing message using the MailMessage class' load() method and specifying the MessageFormat of the existing message.
3. Get the subject using getSubject().
4. After modifying the subject, set it again using the setSubject() method.
5. Get the body using getHtmlBody().
6. AFter modifying the body, set it using the setHtmlBody() method .
7. Create an instance of the MailAddressCollection class.
8. Get the recipients from the TO field into a MailAddressCollection object using the getTo() method exposed by the MailMessage class.
9. Add or remove recipients using the add() and remove() methods exposed by the MailAddressCollection class.
10. Get the recipients from the CC field into a MailAddressCollection object using the getCC() method exposed by MailMessage class.
11. Add or remove recipients using the add() and remove() methods exposed by the MailAddressCollection class.
12. Call the save() method exposed by the MailMessage class, specifying the correct MailMessageSaveType to save the message file to the disk in MSG format.


Copy this code and paste it in your HTML
  1. public static void UpdateAndSaveEmail()
  2. {
  3. // Base folder for reading and writing files
  4. String strBaseFolder = Environment.getExternalStorageDirectory().getPath();
  5. strBaseFolder = strBaseFolder + "/";
  6.  
  7. //Initialize and load an existing MSG file by specifying the MessageFormat
  8. MailMessage email = MailMessage.load(strBaseFolder + "anEmail.msg", MessageFormat.getMsg());
  9.  
  10. //Initialize a String variable to get the Email Subject
  11. String subject = email.getSubject();
  12. //Append some more information to Subject
  13. subject = subject + " This text is added to the existing subject";
  14. //Set the Email Subject
  15. email.setSubject(subject);
  16.  
  17. //Initialize a String variable to get the Email's HTML Body
  18. String body = email.getHtmlBody();
  19. //Apppend some more information to the Body variable
  20. body = body + "<br> This text is added to the existing body";
  21. //Set the Email Body
  22. email.setHtmlBody(body);
  23.  
  24. //Initialize MailAddressCollection object
  25. MailAddressCollection contacts = new MailAddressCollection();
  26.  
  27. //Retrieve Email's TO list
  28. contacts = email.getTo();
  29. //Check if TO list has some values
  30. if (contacts.size() > 0)
  31. {
  32. //Remove the first email address
  33. contacts.remove(0);
  34. //Add another email address to collection
  35. contacts.add("[email protected]");
  36. }
  37. //Set the collection as Email's TO list
  38. email.setTo(contacts);
  39.  
  40. //Initialize MailAddressCollection
  41. contacts = new MailAddressCollection();
  42.  
  43. //Retrieve Email's CC list
  44. contacts = email.getCC();
  45. //Add another email address to collection
  46. contacts.add("[email protected]");
  47. //Set the collection as Email's CC list
  48. email.setCC(contacts);
  49.  
  50. //Save the Email message to disk by specifying the MessageFormat
  51. email.save(strBaseFolder + "message.msg", MailMessageSaveType.getOutlookMessageFormat());

URL: http://www.aspose.com/docs/display/emailandroid/Update+and+Save+an+Email

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.