3

Hi I am getting the following exception

Exception in thread "main" java.lang.ClassCastException: javax.mail.util.SharedByteArrayInputStream cannot be cast to javax.mail.Multipart

I am not getting any compilation exception in Eclipse IDE, but when I am trying build the project I am getting this exception.

After building the project, I am running the project through java -jar, so its not satisfying the if(content instanceof Multipart) condition, but when i am running from the Eclipse IDE its working fine. Any pointers will greatly helpful to me

From the eclipse IDE I'm getting the megssage.getContent() as javax.mail.internet.MimeMultipart@1dc0e7a but when running using the jar file I'm getting the content as javax.mail.util.SharedByteArrayInputStream@2f0d54

Please can you tell me what is the difference between them.

Modified code is:

 InputStream inStream = null;
    if(!message.getContentType().contains("text/plain")){
        Object content = message.getContent();          
        if (message.isMimeType("multipart/*")) {  
            //message.isMimeType("multipart/*")||
            Multipart multipart = (Multipart) content;
            for (int j = 0; j < multipart.getCount(); j++) {
                BodyPart bodyPart = multipart.getBodyPart(j);
                inStream = bodyPart.getInputStream();
                fileName=bodyPart.getFileName();
                } 
        }
        else{
        System.out.println("content not instance of multipart");    
        }`enter code here`  

Please can anyone help me out in solving this issue..

Thanks in advance...

3
  • Are you sure that you're providing the same source? Commented Aug 29, 2011 at 8:27
  • Yes, i am providing the same source .... Commented Aug 29, 2011 at 11:40
  • I am seeing something similar in my project and I noticed the documentation for getContent() states "For content-types that are unknown to the DataHandler system, an input stream is returned as the content." Is it possible that executing the code in a different context creates a message that uses a different DataHandler? Maybe in the OPs case his eclipse classpath finds different versions of the mail classes that construct the Message then his command line classpath does and they instantiate different DataHandlers. Commented May 3, 2012 at 20:16

5 Answers 5

5

message.getContent() returns javax.mail.util.SharedByteArrayInputStream here, but the SharedByteArrayInputStream is not cast-able to a Multipart instance, because you may not necessarily have a multipart message.

You could check if its mimetype is a multipart anything:

if (message.isMimeType("multipart/*") {
    Multipart mp = (Multipart)message.getContent();
    // more stuff
}

Or you can do instance of...

if (message.getContent() instanceof Multipart) {
    Multipart mp = (Multipart)message.getContent();
    // more
}
Sign up to request clarification or add additional context in comments.

Comments

1

You're getting an exception because the return value of getContent is a reference to a javax.mail.util.SharedByteArrayInputStream and that class doesn't implement Multipart. Presumably this isn't a multipart mail message.

As the documentation for Part.getContent states:

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

So basically if you want to handle multipart messages in a particular way, you'll need to use:

Object content = message.getContent();
if (content instanceof Multipart)
{
    Multipart multipart = (Multipart) content;
    // ...
}
else
{
    // Handle non-multipart content
}

1 Comment

Hi, I am getting the content as javax.mail.internet.MimeMultipart@289d2e, when I am running the project from eclipse IDE its working fine and if(content instanceof Multipart) returns true. But when i am trying to build the project and running using the java -jar then its not entering into the if loop and the condition is not satisfying. What is the difference between running from eclipse IDE and through command prompt after building the project..can you please help me
1

Ok, so here is what is happening. It looks like you're trying to get the content from an object which implements javax.mail.Part, but the format is unknown, in which case MimeMessage will return an input stream. In this case, it is returning a javax.mail.util.SharedByteArrayInputStream. Regardless, an input stream cannot be converted to the Multipart interface.

You can test to see if it is an implementer of multipart using isMimeType (birryree's suggestion):

if (message.isMimeType("multipart/*") 
{
    Multipart multipart = (Multipart) content;
    // what you have above.
}
else
{
    // it is not multi-part
}

Or you can test for a direct match (my original suggestion):

// other string comparisons will work here too.
if(message.getContentType().equals("multipart"))
{
     Multipart multipart = (Multipart) message.getContent();
    // what you have above.
}
else
{
    // it is not multi-part
}

getContentType is also on the Part interface. Its documentation can be found here.
You can see a list of all possible content types here.

Or you can test based on the result of instanceof (Jon Skeet's answer):

Object content = message.getContent();
if (content instanceof Multipart)
{
    Multipart multipart = (Multipart) content;
    // what you have above.
}
else
{
    // it is not multi-part
}

1 Comment

Hi, I am getting the content as javax.mail.internet.MimeMultipart@289d2e, when I am running the project from eclipse IDE its working fine and if(content instanceof Multipart) returns true. But when i am trying to build the project and running using the java -jar then its not entering into the if loop and the condition is not satisfying. What is the difference between running from eclipse IDE and through command prompt after building the project..can you please help me
1

For unknown mime-types the MimeMessage class returns with ShraedByteArrayInputstream as the documentation says.

Check the returning type with instanceof then cast.

Update:

If you're using the same source as you did in Eclipse and the response of getContent() method is still different, then you can try modifying the file.encoding property.

Example:

java -Dfile.encoding=UTF8 -jar something.jar

Update2:

Maybe an older version of the loaded class is used in your jar. Please check your classpath for the loaded classes.

1 Comment

Still getContent() method is different from eclipse to command line
0

When you export the Runnable jar file, choose package required libraries into generated JAR, which solves the problem.

This might because some charset can't be found properly, so the returned multipart object is not parsed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.