How can I show a multipart HTML email message in a JEditorPane?
Following is the snippet that I am using to fetch and display emails.
String email = "";
jEditorPane1.setContentType("text/html");
for( int i=0 ; i<msgs.length ; i++ ) {
senSub += InternetAddress.toString(msgs[i].getFrom()) + "\n" + msgs[i].getSubject() + "\n" + "\n";
Object o = msgs[i].getContent();
if( o instanceof String) {
email += o.toString();
System.out.println("FIRST IF");
} else if( o instanceof Multipart ) {
Multipart mp = (Multipart)o;
开发者_如何学Go int bodyParts = mp.getCount();
System.out.println("BODY PARTS---->" + bodyParts ); // There are 2 body parts
for(int j = 0 ; j < bodyParts ; j++) {
BodyPart bodyPart = mp.getBodyPart(j);
Object o2 = bodyPart.getContent();
if( o2 instanceof String ) {
email += "--THIS IS THE " + j + " BODY PART" + "-- " + o2.toString() + "--THIS IS THE END OF FIRST PART--";
} else if( o2 instanceof Multipart ) {
Multipart mp2 = (Multipart)o2;
System.out.println("NESTED MULTIPART");
int count2 = mp2.getCount();
for(int k = 0 ; k < count2 ; k++) {
BodyPart bodyPart1 = mp2.getBodyPart(k);
}
}
}
}
}
Font font = new Font("Trebuchet MS" , Font.BOLD , 15);
jTextArea6.setFont(font);
status = false;
jTextArea6.setText(senSub);
jEditorPane1.setText(email); // set the email on the editor pane
But it is showing some strange behavior.
Here is the output : ( in 2 parts )
part 2And here is the original email:
Let me explain the output. In the first body part
(of multipart message) the plain message
+ links of images
are pasted.The in the second part the images in the original email are pasted. With the line The days of needing.....anywhere repeated twice. And the second part never gets over. Why is this?
There are 7 messages in my inbox but only 1 email is displayed, no doubt the code that fetches the email is somewhere wrong. But I can't figure out where it is wrong. _Let me tell you that I get the right results corresponding to the statement
senSub += InternetAddress.toString(msgs[i].getFrom()) + "\n" + msgs[i].getSubject() + "\n";
result is 7 subjects and 7 senders
Please tell what is wrong with the code.I want links to pasted as link, text to pasted as text, images to pasted as images and not randomly.
精彩评论