Retrieve the HTML content of an email message
In the following task, an exception may be thrown when you invoke the Transport.more()
method.
- 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;
- Create a method that takes a MimeBodyPart object as a parameter.
void readEmailBody(MimeBodyPart mbp)
{
- Invoke MimeBodyPart.getContent() and MimeBodyPart.getContentType() to retrieve the content of the MimeBodyPart object.
Object obj = mbp.getContent();
String mimeType = mbp.getContentType();
- Create a String variable to hold the String representation of the MimeBodyPart object.
String body = null;
- 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;
}
- 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);
}
- 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)
{
- 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())
{
- 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);
- 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)
{
- 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())
{
- 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.