moss
moss

Reputation: 81

Reading MimeMessage - MimeMultipart cannot be cast to MimeMultipart

I have created an application that integrates with email and everything works fine, but when trying to connect to another mail server (after deployment on customer server), I receive a casting error while parsing messages from server.

java.lang.ClassCastException: javax.mail.internet.MimeMultipart can not be cast it javax.mail.internet.MimeMultipart

--

if ( contentType.contains( "text/plain" ) ) {
                content = getFormatedHtmlFromString( object.toString() );
            }
            // check if text/html
            else if ( contentType.contains( "text/html" ) ) {
                content = object.toString();
            }
            else if ( contentType.contains( "multipart" ) ) {

                MimeMultipart mmp = (MimeMultipart) object;

            }

The problem is odd to me because I can parse the message information like date, subjects, etc, but not the content.

Is there only version/standard difference with mail servers and javax api? Is it a problem with javax api, or something with the class loaders on the application server?

Upvotes: 0

Views: 2082

Answers (2)

Elmar Dott
Elmar Dott

Reputation: 117

I got the same issue after I tried to update jakarta.mail from 1.6.x to 2.0.0.

<dependency>
    <groupId>com.sun.mail</groupId>
    <artifactId>jakarta.mail</artifactId>
    <version>2.0.0</version>
</dependency>

1.6 = javax.mail.internet.MimeMessage 2.0 = jakarta.mail.internet.MimeMessage

Both Classes are not compatible. My problem occur, because I also use the greenmail library in the current version 1.6, but greenmail uses intern javax.mail.

import jakarta.mail.internet.MimeMessage;

SMTP_SERVER = new GreenMail(ServerSetupTest.SMTPS);
SMTP_SERVER.start();
SMTP_SERVER.setUser("john.doe@localhost", "JohnDoe", "s3cr3t");
MimeMessage msg = (MimeMessage) SMTP_SERVER.getReceivedMessages();

any try to cast the old MimeMessage to the new Class fail.

<dependency>
    <groupId>com.icegreen</groupId>
    <artifactId>greenmail</artifactId>
    ><version>1.6.1</version>
    <scope>test</scope>
</dependency>

a dirty hack is working in the current situation add the old JavaMail 1.6 dependency in the test scope until greenmail is ready for jakarta mail 2.0

Upvotes: 0

Bill Shannon
Bill Shannon

Reputation: 29961

There's some sort of class path related problem. Perhaps there's two versions of the JavaMail classes available to your application?

Upvotes: 3

Related Questions