Aufrufen von Nur-Text- und HTML-Inhalten im Nachrichtentext einer E-Mail-Nachricht mithilfe einer rekursiven Methode
Erstellen Sie eine rekursive Methode, um alle Teile des Nachrichtentexts einer E-Mail-Nachricht, einschließlich Nur-Text- und HTML-Inhalten, abzurufen.
- Importieren Sie die erforderlichen Klassen und Schnittstellen.
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;
- Erstellen Sie die Methodensignatur für die rekursive Methode.
void findEmailBody(Object obj) {...}
- Erstellen Sie lokale Variablen, die anzeigen, ob der BlackBerry Attachment Service den Nachrichtenanlagentyp unterstützt.
boolean _hasSupportedAttachment;
boolean _hasUnsupportedAttachment;
- Initialisieren Sie die lokalen Variablen.
_hasSupportedAttachment = false;
_hasUnsupportedAttachment = false;
- Wenn der Methodenparameter ein Multipart-Objekt ist, besitzt das Objekt mehrere BodyPart-Objekte. Rufen Sie für jedes BodyPart-Objekt die rekursive Methode auf, die den Nachrichtentext einer E-Mail-Nachricht durchsucht.
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));
}
}
- Wenn das BodyPart-Objekt ein TextBodyPart-Objekt ist, rufen Sie den Nur-Text-Inhalt der Nachricht ab.
else if (obj instanceof TextBodyPart)
{
//This message only has a text body.
TextBodyPart tbp = (TextBodyPart) obj;
readEmailBody(tbp);
}
- Überprüfen Sie, ob das BodyPart-Objekt ein MimeBodyPart-Objekt ist.
else if (obj instanceof MimeBodyPart) {...}
- Wenn das BodyPart-Objekt ein MimeBodyPart-Objekt ist, führen Sie die folgenden Aktionen aus:
- Wandeln Sie das BodyPart-Objekt in ein MimeBodyPart-Objekt um.
MimeBodyPart mbp = (MimeBodyPart) obj;
- Wenn das MimeBodyPart-Objekt keine Anlagen enthält, rufen Sie den Text der Nachricht ab, indem Sie das MimeBodyPart-Objekt als Parameter verwenden.
if (mbp.getContentType().indexOf(ContentType.TYPE_TEXT_HTML_STRING) != -1)
{
readEmailBody(mbp);
}
- Wenn das MimeBodyPart-Objekt Anlagen enthält, rufen Sie eine Methode auf, die den Nachrichtentext der Nachricht abruft.
else if
(mbp.getContentType().equals(ContentType.TYPE_MULTIPART_MIXED_STRING) ||
mbp.getContentType().equals(ContentType.TYPE_MULTIPART_ALTERNATIVE_STRING))
{
findEmailBody(mbp.getContent());
}
- Wenn das BodyPart-Objekt eine Anlage ist, die vom BlackBerry Attachment Service unterstützt wird, ändern Sie die entsprechende lokale Variable in "true".
else if (obj instanceof SupportedAttachmentPart)
{
_hasSupportedAttachment = true;
}
- Wenn das BodyPart-Objekt eine Anlage ist, die nicht vom BlackBerry Attachment Service unterstützt wird, ändern Sie die entsprechende lokale Variable in "false".
else if (obj instanceof UnsupportedAttachmentPart)
{
_hasUnsupportedAttachment = true;
}
Codebeispiel: Abrufen des Inhalts einer E-Mail-Nachricht
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;
}
}
Waren diese Informationen hilfreich? Senden Sie uns Ihren Kommentar.