• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Load Existing Email Message & Modify its Contents inside Android Apps

 
Ranch Hand
Posts: 714
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 Create an instance of the MailMessage class and Load an existing message using the MailMessage class' load() method and specifying the MessageFormat of the existing message. Get the subject using getSubject(). After modifying the subject, set it again using the setSubject() method. Get the body using getHtmlBody(). After modifying the body, set it using the setHtmlBody() method. After that create an instance of the MailAddressCollection class and get the recipients from the TO field into a MailAddressCollection object using the getTo() method exposed by the MailMessage class. Add or remove recipients using the add () and remove () methods exposed by the MailAddressCollection class and get the recipients from the CC field into a MailAddressCollection object using the getCC() method exposed by MailMessage class. Add or remove recipients using the add() and remove() methods exposed by the MailAddressCollection class. Call the save() method exposed by the MailMessage class, specifying the correct MailMessageSaveType to save the message file to the disk in MSG format.

Modifying an Existing Email Message


public static void UpdateAndSaveEmail()

{

    // Base folder for reading and writing files

    String  strBaseFolder  = Environment.getExternalStorageDirectory().getPath();

    strBaseFolder = strBaseFolder + "/";


    //Initialize and load an existing MSG file by specifying the MessageFormat

    MailMessage email = MailMessage.load(strBaseFolder + "anEmail.msg", MessageFormat.getMsg());


    //Initialize a String variable to get the Email Subject

    String subject = email.getSubject();

    //Append some more information to Subject

    subject = subject + " This text is added to the existing subject";

    //Set the Email Subject

    email.setSubject(subject);


    //Initialize a String variable to get the Email's HTML Body

    String body = email.getHtmlBody();

    //Apppend some more information to the Body variable

    body = body + "<br> This text is added to the existing body";

    //Set the Email Body

    email.setHtmlBody(body);


    //Initialize MailAddressCollection object

    MailAddressCollection contacts = new MailAddressCollection();


    //Retrieve Email's TO list

    contacts = email.getTo();

    //Check if TO list has some values

    if (contacts.size() > 0)

    {

        //Remove the first email address

        contacts.remove(0);

        //Add another email address to collection

        contacts.add("to1@domain.com");

    }

    //Set the collection as Email's TO list

    email.setTo(contacts);


    //Initialize MailAddressCollection

    contacts = new MailAddressCollection();


    //Retrieve Email's CC list

    contacts = email.getCC();

    //Add another email address to collection

    contacts.add("cc2@domain.com");

    //Set the collection as Email's CC list

    email.setCC(contacts);


    //Save the Email message to disk by specifying the MessageFormat

    email.save(strBaseFolder + "message.msg", MailMessageSaveType.getOutlookMessageFormat());



Overview: Aspose.Email for Android

Aspose.Email for Android API enables developers to design Android applications for managing & manipulating Outlook email file formats without using MS Outlook. It provides tools to create, read & convert Outlook MSG, PST, EML, EMLX, OST & MHT file formats. It manages recipients, subject, message body, attachments in MSG documents. It creates & save appointments in draft format, extract & save calendar items from a PST, add Mapi Items such as Messages, Contacts, Notes, Journals & Tasks to a PST.

More about Aspose.Email for Android

Contact Information


Suite 119, 272 Victoria Avenue

Chatswood, NSW, 2067

Australia

Aspose - Your File Format Experts

sales@aspose.com


Phone: 888.277.6734

Fax: 866.810.9465


 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic