Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Access HTML email messages

by BlackBerry Development Advisor ‎02-16-2010 11:49 AM - edited ‎02-17-2010 03:40 PM (3,308 Views)

Summary


This article applies to the following:

  • BlackBerry® smartphones based on Java® technology


Details


BlackBerry® Device Software 4.5 supports the display of richly rendered HTML email messages, when used with BlackBerry® Enterprise Server software version 4.1 Service Pack 6 (4.1.6) or BlackBerry® Internet Service 2.5. This means that the BlackBerry smartphone may receive an email message body that contains HTML, plain text, or both.


Third-party applications can access the body of the email message in both HTML and plain text format. The HTML body can be accessed using a MimeBodyPart and the plain text body can be accessed by using a TextBodyPart, both of which are packaged inside a MulitPart object.

The hierarchy of an email message can vary based on the content within the email message. Factors include whether the email body is in HTML or plain text format (or both), whether the email message contains a type of attachment supported by the BlackBerry Attachment Service, or whether the email message contains a type of attachment that is not supported by the BlackBerry Attachment Service. A list of supported attachments can be found in this article.


Because the hierarchy of an email message can vary, it is recommended that an application be able to adapt to any hierarchy structure. The following class demonstrates such an implementation and shows how to extract the HTML and plain text content from an email message. A complete working sample that includes this class can be downloaded here.



public class MessageViewScreen extends MainScreen
{
private boolean _hasSupportedAttachment;
private boolean _hasUnsupportedAttachment;
private String _plainTextMessage = "";
private String _htmlMessage = "";

public MessageViewScreen(Message message)
{
super();
findEmailBody(message.getContent());

if (_hasSupportedAttachment)
{
this.add(new LabelField("This message has a supported attachment."));
}
else if (_hasUnsupportedAttachment)
{
this.add(new LabelField("This message has an unsupported attachment of type."));
}
else
{
this.add(new LabelField("This message has no attachment."));
}

this.add(new SeparatorField());
this.add(new EditField("Plain Text Message Body: ", _plainTextMessage));
this.add(new SeparatorField());
this.add(new EditField("HTML Message Body: ", _htmlMessage));
}

//This method reads within an email message to find the email body.
private void findEmailBody(Object obj)
{
//Reset the attachment flags.
_hasSupportedAttachment = false;
_hasUnsupportedAttachment = false;

if(obj instanceof Multipart)
{
Multipart mp = (Multipart)obj;

//Extract all of the parts within the Multipart message.
for(int count=0; count < mp.getCount(); ++count)
{
findEmailBody(mp.getBodyPart(count));
}
}
else if (obj instanceof TextBodyPart)
{
//This message only has a text body.
TextBodyPart tbp = (TextBodyPart)obj;
readEmailBody(tbp);
}
else if (obj instanceof MimeBodyPart)
{
MimeBodyPart mbp = (MimeBodyPart)obj;

if (mbp.getContentType().indexOf(ContentType.TYPE_TEXT_HTML_STRING) != -1)
{
//The message has no attachments.
//Read the email body, which may contain a
//TexBodyPart, MimeBodyPart or both.
readEmailBody(mbp);
}
else if
(mbp.getContentType().equals(ContentType.TYPE_MULTIPART_MIXED_STRING) ||

mbp.getContentType().equals(ContentType.TYPE_MULTIPART_ALTERNATIVE_STRING))
{
//The message has attachments or we are at the
//top level of the message. Dig deeper to find the body.
//Extract all of the parts within the MimeBodyPart message.
findEmailBody(mbp.getContent());
}

}
else if (obj instanceof SupportedAttachmentPart)
{
//This is a supported attachment.
_hasSupportedAttachment = true;
}
else if (obj instanceof UnsupportedAttachmentPart)
{
//This is an unsupported attachment.
_hasUnsupportedAttachment = true;
}
}

private void readEmailBody(TextBodyPart tbp)
{
//This is the plain text body.
_plainTextMessage = (String)tbp.getContent();

//Determine if all of the text body part is present.
if (tbp.hasMore() && !tbp.moreRequestSent())
{
//It does, request more of the message.
try
{
Transport.more((BodyPart)tbp, true);
Status.show("Requesting more of the plain text message body. Reopen the screen to view it once more has been received.");
}
catch (Exception ex)
{
Dialog.alert("Exception: " + ex.toString());
}
}
}

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

//Determine if the data returned is a String or a byte array.
//If the BlackBerry is able to convert the HTML content
//into a String, then a String should be returned. If
//the encoding is not supported a byte array is returned
//to allow your application to work with the raw data.

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

if (mimeType.indexOf(ContentType.TYPE_TEXT_PLAIN_STRING) != -1)
{
//This is the plain text body.
_plainTextMessage = body;

//Determine if all of the text body part is present.
if (mbp.hasMore() && !mbp.moreRequestSent())
{
//It does, request more of the message.
try
{
Transport.more((BodyPart)mbp, true);
Status.show("Requesting more of the plain text message body. Reopen the screen to view it once more has been received.");
}
catch (Exception ex)
{
Dialog.alert("Exception: " + ex.toString());
}
}
}
else if (mimeType.indexOf(ContentType.TYPE_TEXT_HTML_STRING) != -1)
{
//This is the HTML body part of the message.
_htmlMessage = body;

//Determine if all of the HTML body part is present.
if (mbp.hasMore() && !mbp.moreRequestSent())
{
//It does, request more of the message.
try
{
Transport.more((BodyPart)mbp, true);
Status.show("Requesting more of the HTML message body. Reopen the screen to view it once more has been received.");
}
catch (Exception ex)
{
Dialog.alert("Exception: " + ex.toString());
}
}
}
}
}




Contributors
Users Online
Currently online: 21 members 1,096 guests
Please welcome our newest community members: