Alternative for apache commons-email and Jakarta

1.2k views Asked by At

We've upgraded our application to Spring6/Tomcat10 (that implies migration from Java EE to Jakarta) and could not find jakarta compatible alternative for apache commons-email library?!

We seem to have decent amount of custom code that uses commons-email. Have anyone else run into this issue before? What our options are?

After performing the initial research I can think of the following options (and to be honest I don't like neither one of them :-():

  1. Refactor our code to use jakarta.mail where commons-email library is used? (Seem to be a significant effort).
  2. Fork source code of the commons-email library and migrate it to jakarta ourselves? (Have anyone taken this route? If it's that straight-forward wondering why maintainer of the commons-email project have not done that already?)
  3. We tried to keep both javax-mail and jakarta-mail libraries in the classpath, and keep using commons-email in the places where it's used and it works most of the time, but sporadically throws:

java.lang.ClassCastException: class com.sun.mail.handlers.multipart_mixed cannot be cast to class jakarta.activation.DataContentHandler (com.sun.mail.handlers.multipart_mixed and jakarta.activation.DataContentHandler are in unnamed module of loader 'app')

Is there anything else we can do? Any help with be greatly appreciated!

4

There are 4 answers

1
A. Assaad On

You can clone the commons-email repo and compile it locally. It's at version 1.6-SNAPSHOT, and seems to depend on jakarta already. I'd update the dependencies while you're at it. To illustrate, I copied MimeMessageParser from version 1.5 and replaced javax with jakarta in the imports, and that works with a one-to-one correspondence for the javax/jakarta classes I looked at. When (for instance) mvnrepository catches up with version 1.6 you can go back to using that.

2
Olivier Masseau On

commons-email 2.0.0 which will be compatible with the jakarta namespace will be available soon (I'm writing this answer on 2024-01-15) as you can see in this Jira ticket : https://issues.apache.org/jira/browse/EMAIL-203

0
whitenexx On

If it's okay for you, you could use the the milestone snapshot version of apache commons mail especially for jakarta.

Just activate the apache snapshot maven repository within your maven or gradle file and use the following dependency:

repositories {
    mavenCentral()

    // add the snapshot repo
    maven { url 'https://repository.apache.org/content/repositories/snapshots/' }
}

Add the depenency:

dependencies {
.....
implementation group: 'com.sun.mail', name: 'jakarta.mail', version: '2.0.1'

// the snapshot version of commons-email for jakarta
implementation 'org.apache.commons:commons-email2-jakarta:2.0.0-M1-SNAPSHOT'
.....
}
0
David Shotkin On

Try using:

<!-- https://mvnrepository.com/artifact/com.github.ppodgorsek.email/commons-email-jakarta -->
<dependency>
    <groupId>com.github.ppodgorsek.email</groupId>
    <artifactId>commons-email-jakarta</artifactId>
    <version>2.0.0</version>
</dependency>