📄 messageviewscreen.java
字号:
package mailReader;
import net.rim.blackberry.api.mail.BodyPart;
import net.rim.blackberry.api.mail.BodyPart.ContentType;
import net.rim.blackberry.api.mail.Message;
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.Transport;
import net.rim.blackberry.api.mail.UnsupportedAttachmentPart;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.component.Status;
import net.rim.device.api.ui.container.MainScreen;
import java.util.*;
//This screen accepts a message as a parameter of which it traverses
//through each part of the message, extracting the plain text and
//HTML body (if available) and determines if the message has an
//email attachment.
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;
SupportedAttachmentPart sap=(SupportedAttachmentPart) obj;
String fName = sap.getName();
Status.show("ContentType : "+sap.getContentType());
if (sap.hasMore() && !sap.moreRequestSent())
{
//It does, request more of the message.
}
byte[] content = (byte[])sap.getContent();
Status.show("Size : "+content.length);
}
else if (obj instanceof UnsupportedAttachmentPart)
{
//This is an unsupported attachment.
_hasUnsupportedAttachment = true;
UnsupportedAttachmentPart usap=(UnsupportedAttachmentPart) obj;
String fName = usap.getName();
if(fName.equals("x-rimdevice-logo_ar.png")){
Enumeration enum = usap.getAllHeaders();
while(enum.hasMoreElements()){
Status.show("Header : "+enum.nextElement());
}
if (usap.hasMore() && !usap.moreRequestSent())
{
//It does, request more of the message.
try
{
Transport.more((BodyPart)usap, true);
Status.show("Requesting more of : "+fName);
}
catch (Exception ex)
{
Dialog.alert("Exception: " + ex.toString());
}
}
byte[] content = (byte[])usap.getContent();
Status.show("Size : "+content.length);
}
}
}
private void readEmailBody(TextBodyPart tbp)
{
//This is the plain text body.
_plainTextMessage = (String)tbp.getContent();
Status.show("TextMessage :"+_plainTextMessage);
//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());
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -