It's not a secret anymore!
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Class Cast Exception" Watch "Class Cast Exception" New topic

Class Cast Exception

Robert Young

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);

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
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:
subject: Class Cast Exception
jQuery in Action, 3rd edition