Retrieve the HTML 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.MimeBodyPart;
    import net.rim.blackberry.api.mail.Transport;
    import net.rim.device.api.ui.component.Dialog;
    
  2. Create a method that takes a MimeBodyPart object as a parameter.
    void readEmailBody(MimeBodyPart mbp)
    {
  3. Invoke MimeBodyPart.getContent() and MimeBodyPart.getContentType() to retrieve the content of the MimeBodyPart object.
    Object obj = mbp.getContent();
    String mimeType = mbp.getContentType();
  4. Create a String variable to hold the String representation of the MimeBodyPart object.
    String body = null;
  5. If the BlackBerry® device can convert the HTML body of a message to a String, the MimeBodyPart object will be a String. Cast the MimeBodyPart object as a String and assign it to the String representation of the body part of the message.
    if (obj instanceof String)
    {
       body = (String)obj;
    }
  6. If the BlackBerry device cannot convert the HTML body of a message to a String, the MimeBodyPart object will be a byte array. Create a new instance of String using as a parameter the MimeBodyPart object cast as a byte array. Assign the String to the String representation of the body part of the message.
    else if (obj instanceof byte[])
    {
       body = new String((byte[])obj);
    }
  7. Check to see if the String representation of the content of the MimeBodyPart object contains ContentType.TYPE_TEXT_PLAIN_STRING to determine if the MimeBodyPart object is the plain text body part of the message.
    if (mimeType.indexOf(ContentType.TYPE_TEXT_PLAIN_STRING) != -1)
    {
  8. Invoke MimeBodyPart.hasMore() and MimeBodyPart.moreRequestSent() to determine if all of the text body part is present in the MimeBodyPart object.
    if (mbp.hasMore() && !mbp.moreRequestSent())
    {
     
  9. If more data is available for the MimeBodyPart object, invoke Transport.more() to retrieve the rest of the data for the MimeBodyPart object.
    Transport.more((BodyPart)mbp, true);
    
  10. Check to see if the String representation of the content of the MimeBodyPart object contains ContentType.TYPE_TEXT_HTML_STRING to detemine if the MimeBodyPart object is the HTML body part of the message.
    else if (mimeType.indexOf(ContentType.TYPE_TEXT_HTML_STRING) != -1)
    {
  11. Invoke MimeBodyPart.hasMore() and MimeBodyPart.moreRequestSent() to determine if all of the HTML body part is present in the MimeBodyPart object.
    if (mbp.hasMore() && !mbp.moreRequestSent())
    {
  12. If more data is available for the MimeBodyPart object, invoke Transport.more() to retrieve the rest of the MimeBodyPart object.
    Transport.more((BodyPart)mbp, true);
    

Code sample: Retrieving the HTML content of an email message

private void readEmailBody(MimeBodyPart mbp)
{
   //Extract the content of the message.
   Object obj = mbp.getContent();
   String mimeType = mbp.getContentType();
   String body = null;

   if (obj instanceof String)
   {
      body = (String)body;
   }
   else if (obj instanceof byte[])
   {
      body = new String((byte[])obj);
   }

   if (mimeType.indexOf(ContentType.TYPE_TEXT_PLAIN_STRING) != -1)
   {
      _plainTextMessage = body;

      //Determine if all of the text body part is present.
      if (mbp.hasMore() && !mbp.moreRequestSent())
      {
         try
         {
            Transport.more((BodyPart)mbp, true);
         }
         catch (Exception ex)
         {
            Dialog.alert("Exception: " + ex.toString());
         }
      }
   }
  
   else if (mimeType.indexOf(ContentType.TYPE_TEXT_HTML_STRING) != -1)
   {
      _htmlMessage = body;

      //Determine if all of the HTML body part is present.
      if (mbp.hasMore() && !mbp.moreRequestSent())
      {
         try
         {
            Transport.more((BodyPart)mbp, true);
         }
         catch (Exception ex)
         { 
            Dialog.alert("Exception: " + ex.toString());
         }
      }
   }
}

Was this information helpful? Send us your comments.