update jakarta.mail 1.6.5 to 2.0.1

Viewed 1459

I'm trying to migrate from JakartaMail 1.6.5 to 2.0.1, and when I'm starting my application I got

Caused by: java.lang.ClassNotFoundException: javax.mail.internet.AddressException
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1364)

I also try to change:

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

to

import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;

However, then when I use JavaMailSenderImpl mailSender of Spring class like:

 MimeMessage message = mailSender.createMimeMessage();

It says:

Required type:
jakarta.mail.internet.MimeMessage
Provided:
javax.mail.internet.MimeMessage

How can I solve this? Does Spring support JakartaMail 2.0.1? because JavaMailSender is in spring-context-support (I use version 5.3.9)

1 Answers

The problem is that JakartaMail 2.x switched to the new jakarta.mail namespace. As a result, libraries built against earlier versions of JakartaMail/JavaMail will not work as they expect the javax.mail namespace.

Spring still uses the javax.mail namespace and thus cannot be used in combination with JakartaMail 2.x. In fact, the current main of Spring Framework is compiled against com.sun.mail:javax.mail:1.6.2/javax.mail:javax.mail-api:1.6.2 (see build.gradle).

For now, if you want to use Spring, you need to use JakartaMail 1.6.7 or earlier. As far as I know, version 1.6.7 and 2.0.1 are identical (or nearly identical), except for the difference in namespace.

According to Support for Jakarta EE 9 (annotations and interfaces in jakarta.* namespace), Spring will switch to using the jakarta.* namespace in Spring Framework 6 / Spring Boot 3.

Related