aspose file tools*
The moose likes Beginning Java and the fly likes Class Cast Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Class Cast Exception" Watch "Class Cast Exception" New topic
Author

Class Cast Exception

Robert Young
Greenhorn

Joined: Jun 09, 2003
Posts: 4
I am extracting the content from an email message. I am casting this into a string object. However, when it is a multipart message and it gets to the text/html content I get a class cast exception. Does anyone know what type of object is being returned since it is not a string.

I can't seem to find anything about it.

Below is the method:

private String extractContent(MimeMessage msg) throws MessagingException, IOException {
//extract content
StringBuffer sb = new StringBuffer(1000);
try{

if (msg.getContentType().indexOf("text/plain") != -1) {
//plain text message
sb.append((String) msg.getContent());
} else if (msg.getContentType().indexOf("text/html") != -1) {
sb.append((String) msg.getContent());
} else if (msg.getContentType().indexOf("multipart") != -1) {
Multipart mp = (Multipart) msg.getContent();
for (int i = 0; i < mp.getCount(); i++) {
Part part = mp.getBodyPart(i);
if (part.getContentType().indexOf("text/plain") != -1) {
sb.append((String) part.getContent());
} else if (part.getContentType().indexOf("text/html") != -1) {
sb.append((String) part.getContent());
}
}
}
}catch(UnsupportedEncodingException e){
Greg T Robertson
Ranch Hand

Joined: Nov 18, 2003
Posts: 91
If you really wanna know you can do a

or something very similar to that basically you get the class and then it's name and you can tell exactly what you have.
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Usually the detail message of the CCE will tell you. Not always though for some reason.

- Peter
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Class Cast Exception