java.lang.ClassCastException: com.sun.mail.util.SharedByteArrayInputStream

I want to cast an Object to a javax.mail.Multipart

Compilation Ok

But i have this message : java.lang.ClassCastException: com.sun.mail.util.SharedByteArrayInputStream when using the bean with JSP

Here are the sources :

public Monmail(Message mess) throws IOException ,MessagingException {

DebugWriter writer = new DebugWriter();

try{

adresse = "de " + mess.getFrom()[0] + "\n";

suj = mess.getSubject();

cont = (Multipart)mess.getContent();

int i = cont.getCount();

Part part = null;

for (int j=0;j<i;j++){

part = cont.getBodyPart(j);

writer.writeDebug(part.getContentType());

}

typr = part.getContentType();

}catch (MessagingException ex){

writer.writeDebug("Messaging exception dans Monmail");

}

}

What can I do? I absolutely need to get the attachements to use it with JSP

Thanks

AgilBull

>

[973 byte] By [agilbull] at [2007-9-26 1:17:49]
# 1

A suggestion:

Since mess.getContent() returns an Object your compiler won't complain if you want to cast it (see class Part which is superclass of Message). If your actual object at runtime is not a Multipart or a superclass of Multipart then you have a problem. Which is the case here!

Indeed the java api states the following:

public java.lang.Object getContent() throws java.io.IOException, MessagingException

Return the content as a Java object. The type of the returned object is of course dependent on the content itself. For example, the object returned for "text/plain" content is usually a String object. The object returned for a "multipart" content is always a Multipart subclass. For content-types that are unknown to the DataHandler system, an input stream is returned as the content.

Suggestion: find out which object is actually there and then do the correct cast

e.g. use

Object cont = mess.getContent()

if (cont instanceof Multipart)

{

((Multipart)cont).getCount

}

and check other possibilities as well!

Hope it helps,

Filip

filipicom at 2007-6-29 0:47:56 > top of Java-index,Archived Forums,Java Programming...
# 2
Thanks, your suggestion seemes to be the good one.TchoCedric
agilbull at 2007-6-29 0:47:56 > top of Java-index,Archived Forums,Java Programming...
# 3

So finally this suggestion is not the good one.

Here is the new code :

if (mess.isMimeType("multipart/*")) {

writer.writeDebug("le mimetype est multipart");

cont = mess.getContent();

if (cont instanceof Multipart)

{

writer.writeDebug("cont est une instance de multipart");

nb = ((Multipart)cont).getCount();

}else{

writer.writeDebug("cont n'est pas une instance de multipart");

}

}else{

writer.writeDebug("le mimetype n'est pas multipart");

}

It seems that cont will never be an instance of Multipart.

Without the check (if (cont instanceof Multipart)) using nb = ((Multipart)cont).getCount() occures the first error.

agilbull at 2007-6-29 0:47:56 > top of Java-index,Archived Forums,Java Programming...
# 4
W
agilbull at 2007-6-29 0:47:56 > top of Java-index,Archived Forums,Java Programming...
# 5

Of course it is always preferable to not use the instance of operator and use other methods. Like ones that give you more information about the content type. Without knowing the context of your situation nor expierience with the javax.mail packages I suggested just that.

Point that I tried to make is that the javadoc api gives good information on such a problem. Reading it is good practice.

filipicom at 2007-6-29 0:47:56 > top of Java-index,Archived Forums,Java Programming...
# 6

It seems that the problem occurs if you package everything including JavaMail into a Jar file.

The problem is with the line Multipart mp = (Multipart)aMailMessage.getContent();

It returns a SharedByteArrayInputStream instead of a MultiPart message when you use it from a JAR file.

This code works outside a JAR file. Try it! For further details see http://seminars.jguru.com/forums/view.jsp?EID=484154

Anyone got a solution?

kanemars at 2007-6-29 0:47:56 > top of Java-index,Archived Forums,Java Programming...
# 7

It seems that it is not the right answer because I am not using jar files.

But I tried so much things that my libraries are at least unzipped in two directories.

And I import lots of libraries.

Could you please send me the libraries with their path that you include to receive mail.

Thanks

Cedric

agilbull at 2007-6-29 0:47:56 > top of Java-index,Archived Forums,Java Programming...
# 8
I have the answer,you must use javamail1.1.3 and not javamail1.2Cedric
agilbull at 2007-6-29 0:47:56 > top of Java-index,Archived Forums,Java Programming...
# 9
This actually has to deal with the activation.jar. Try getting the latest one.
psuskeels at 2007-6-29 0:47:56 > top of Java-index,Archived Forums,Java Programming...
# 10

> This actually has to deal with the activation.jar.

> Try getting the latest one.

I'm having the same problem...I just downloaded the latest activation.jar (jaf-1.0.2)...and the problem is still occuring. did you have to do something special in the code to fix it with the new activation.jar?

ScottTM at 2007-6-29 0:47:56 > top of Java-index,Archived Forums,Java Programming...
# 11

I got around the problem with quite simple solution:

MimeMessage message = new MimeMessage(session, message_stream);

MimeMultipart multipart = new MimeMultipart(message.getDataHandler().getDataSource());

It hasn't been tested well, but before I forgot it I decided to post this anyway.

tuomas_kiviaho at 2007-6-29 0:47:56 > top of Java-index,Archived Forums,Java Programming...