Retrieve the plain text and HTML content in the body of an email message using a recursive method
Create a recursive method to retrieve all the parts of the body of an email message including plain text and HTML.
- Import the required classes and interfaces.
import net.rim.blackberry.api.mail.MimeBodyPart;
import net.rim.blackberry.api.mail.Multipart;
import net.rim.blackberry.api.mail.SupportedAttachmentPart;
import net.rim.blackberry.api.mail.TextBodyPart;
import net.rim.blackberry.api.mail.UnsupportedAttachmentPart;
- Create the method signature for the recursive method.
void findEmailBody(Object obj) {...}
- Create local variables that indicate if the BlackBerry® Attachment Service supports the message attachment type.
boolean _hasSupportedAttachment;
boolean _hasUnsupportedAttachment;
- Initialize the local variables.
_hasSupportedAttachment = false;
_hasUnsupportedAttachment = false;
- If the method parameter is a Multipart
object, the object has multiple BodyPart
objects. On each BodyPart
object, invoke the recursive method that searches through the body of an email message.
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));
}
}
- If the BodyPart
object
is a TextBodyPart, retrieve the plain text body of the message.
else if (obj instanceof TextBodyPart)
{
//This message only has a text body.
TextBodyPart tbp = (TextBodyPart) obj;
readEmailBody(tbp);
}
- Check if the BodyPart
object
is a MimeBodyPart.
else if (obj instanceof MimeBodyPart) {...}
- If the BodyPart
object
is a MimeBodyPart, perform the following actions:
- Cast the BodyPart
object
as a MimeBodyPart.
MimeBodyPart mbp = (MimeBodyPart) obj;
- If the MimeBodyPart object does not contain attachments, retrieve the body of the message using the MimeBodyPart object as a parameter.
if (mbp.getContentType().indexOf(ContentType.TYPE_TEXT_HTML_STRING) != -1)
{
readEmailBody(mbp);
}
- If the MimeBodyPart object does contain attachments, invoke a method that retrieves the body of the message.
else if
(mbp.getContentType().equals(ContentType.TYPE_MULTIPART_MIXED_STRING) ||
mbp.getContentType().equals(ContentType.TYPE_MULTIPART_ALTERNATIVE_STRING))
{
findEmailBody(mbp.getContent());
}
- If the BodyPart
is an attachment that the BlackBerry Attachment Service supports, change the appropriate local variable to true.
else if (obj instanceof SupportedAttachmentPart)
{
_hasSupportedAttachment = true;
}
- If the BodyPart
is an attachment that the BlackBerry Attachment Service does not support, change the appropriate local variable to true.
else if (obj instanceof UnsupportedAttachmentPart)
{
_hasUnsupportedAttachment = true;
}
Code sample: Retrieving the content of an email message
private void findEmailBody(Object obj)
{
//Reset the attachment flags.
_hasSupportedAttachment = false;
_hasUnsupportedAttachment = false;
if(obj instanceof Multipart)
{
Multipart mp = (Multipart)obj;
for(int count=0; count < mp.getCount(); ++count)
{
findEmailBody(mp.getBodyPart(count));
}
}
else if (obj instanceof TextBodyPart)
{
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)
{
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.
//Extract all of the parts within the MimeBodyPart message.
findEmailBody(mbp.getContent());
}
else if (obj instanceof SupportedAttachmentPart)
{
_hasSupportedAttachment = true;
}
else if (obj instanceof UnsupportedAttachmentPart)
{
_hasUnsupportedAttachment = true;
}
}
Was this information helpful? Send us your comments.