Retrieve the plain text content of an email message

In the following task, an exception may be thrown when you invoke the Transport.more() method.

  1. Import the required classes and interfaces.
    import net.rim.blackberry.api.mail.BodyPart;
    import net.rim.blackberry.api.mail.TextBodyPart;
    import net.rim.blackberry.api.mail.Transport;
    import net.rim.device.api.ui.component.Dialog;
    
  2. Create a method that takes a TextBodyPart object as a parameter.
    void readEmailBody(TextBodyPart tbp);
  3. Cast the value that TextBodyPart.getContent() returns as a String to get the plain text part of the body of the message.
    _plainTextMessage = (String)tbp.getContent();
  4. Invoke TextBodyPart.hasMore() and TextBodyPart.moreRequestSent() to identify if more of the TextBodyPart object is available on the server.
    if (tbp.hasMore() && !tbp.moreRequestSent())
    {
  5. If more data is available for the TextBodyPart object, invoke Transport.more() to retrieve the rest of the TextBodyPart object.
    Transport.more((BodyPart)tbp, true);
    

Code sample: Retrieving the plain text content of an email message

private void readEmailBody(TextBodyPart tbp)
{
   _plainTextMessage = (String)tbp.getContent();

   if (tbp.hasMore() && !tbp.moreRequestSent())
   {
      try
      {
         Transport.more((BodyPart)tbp, true);
      }
      catch (Exception ex)
      {
         Dialog.alert("Exception: " + ex.toString());
      }
   }
}   

Was this information helpful? Send us your comments.