Récupérer le texte brut et le contenu HTML dans le corps d'un e-mail à l'aide d'une méthode récurrente
Créez une méthode récurrente pour récupérer tous les éléments du corps d'un e-mail y compris en texte brut et HTML.
- Importez les classes et les interfaces requises.
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;
- Créez la signature de méthode de la méthode récurrente.
void findEmailBody(Object obj) {...}
- Créez des variables locales indiquant si BlackBerry Attachment Service prend en charge le type de pièce jointe.
boolean _hasSupportedAttachment;
boolean _hasUnsupportedAttachment;
- Initialisez les variables locales.
_hasSupportedAttachment = false;
_hasUnsupportedAttachment = false;
- Si le paramètre de méthode est un objet Multipart l'objet possède plusieurs objets BodyPart. Sur chaque objet BodyPart, appelez la méthode récurrente recherchant dans le corps d'un e-mail.
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));
}
}
- Si l'objet BodyPart est un objet TextBodyPart, récupérez le corps en texte brut du message.
else if (obj instanceof TextBodyPart)
{
//This message only has a text body.
TextBodyPart tbp = (TextBodyPart) obj;
readEmailBody(tbp);
}
- Vérifiez si l'objet BodyPart est un objet MimeBodyPart.
else if (obj instanceof MimeBodyPart) {...}
- Si l'objet BodyPart est un objet MimeBodyPart, exécutez les actions suivantes :
- Convertissez l'objet BodyPart en objet MimeBodyPart.
MimeBodyPart mbp = (MimeBodyPart) obj;
- Si l'objet MimeBodyPart ne contient pas de pièce jointe, récupérez le corps du message à l'aide de l'objet MimeBodyPart comme paramètre.
if (mbp.getContentType().indexOf(ContentType.TYPE_TEXT_HTML_STRING) != -1)
{
readEmailBody(mbp);
}
- Si l'objet MimeBodyPart contient des pièces jointes, appelez une méthode qui récupère le corps du message.
else if
(mbp.getContentType().equals(ContentType.TYPE_MULTIPART_MIXED_STRING) ||
mbp.getContentType().equals(ContentType.TYPE_MULTIPART_ALTERNATIVE_STRING))
{
findEmailBody(mbp.getContent());
}
- Si l'objet BodyPart est une pièce jointe prise en charge par BlackBerry Attachment Service, définissez la variable locale appropriée sur true (vrai).
else if (obj instanceof SupportedAttachmentPart)
{
_hasSupportedAttachment = true;
}
- Si l'objet BodyPart est une pièce jointe non prise en charge par BlackBerry Attachment Service, définissez la variable locale appropriée sur true (vrai).
else if (obj instanceof UnsupportedAttachmentPart)
{
_hasUnsupportedAttachment = true;
}
Échantillon de code : récupération du contenu d'un e-mail
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;
}
}
Ces informations vous ont-elles été utiles ? Envoyez-nous vos commentaires.