[Fixed] Spring-boot-starter-mail 3.1.1 throws "Not provider of jakarta.mail.util.StreamProvider was found" – Spring

by
Ali Hasan
jakarta-mail spring spring-boot

Quick Fix: Quick Fix: To resolve this issue, manipulate the context class loader during the email sending process. Here’s an example using JavaMailSender:

@Service
public class MailerService {  
  @Autowired
  public JavaMailSender javaMailSender;  
  public void sendEmail(String toAddress, String subject, String body, boolean error) throws MessagingException {  
    MimeMessagePreparator preparator = mimeMessage -> {  
      final Address recipient = new InternetAddress(toAddress);  
      mimeMessage.setFrom(new InternetAddress(fromAddress));  
      mimeMessage.setRecipient(Message.RecipientType.TO, recipient);  
      mimeMessage.setSentDate(new Date());  
      mimeMessage.setSubject(subject);  
      mimeMessage.setText(body);  
    };  
    // Send the e-mail  
    Thread t = Thread.currentThread();  
    ClassLoader orig = t.getContextClassLoader();  
    t.setContextClassLoader(InternetAddress.class.getClassLoader());  
    try {  
      javaMailSender.send(preparator);  
    } finally {  
      t.setContextClassLoader(orig);  
    }  
  }  
}  

The Problem:

An exception occurs when trying to send an email using JavaMailSender. Reason for the exception is that the provider for jakarta.mail.util.StreamProvider could not be found.

The Solutions:

Solution 2: Resolve Dependency Conflict

The error "Not provider of jakarta.mail.util.StreamProvider was found" is typically caused by a conflict between different versions of the Jakarta Mail API dependency on the classpath. The following steps will help resolve this issue:

  1. Identify the conflicting dependencies: Use a dependency management tool such as Maven or Gradle to identify any conflicting Jakarta Mail API dependencies.

  2. Exclude the conflicting dependency: If possible, exclude the conflicting dependency that is not explicitly required by your application. For example, if you have the following dependencies:

    <dependency>
        <groupId>jakarta.mail</groupId>
        <artifactId>jakarta.mail-api</artifactId>
        <version>2.0.1</version>
    </dependency>
    <dependency>
        <groupId>com.sun.mail</groupId>
        <artifactId>jakarta.mail</artifactId>
        <version>2.1.0</version>
    </dependency>
    

    You can exclude the conflicting dependency by adding the following to your pom.xml file:

    <dependency>
        <groupId>com.sun.mail</groupId>
        <artifactId>jakarta.mail</artifactId>
        <version>2.1.0</version>
        <scope>provided</scope>
    </dependency>
    

    This will exclude com.sun.mail:jakarta.mail from the classpath.

  3. Update the Jakarta Mail API dependency version: If excluding the conflicting dependency is not possible, consider updating the version of the Jakarta Mail API to the latest version that is compatible with your application. For example, if you are using Jakarta Mail API 2.0.1, you can update it to Jakarta Mail API 2.1.2 by changing the version in your pom.xml file to:

    <dependency>
        <groupId>jakarta.mail</groupId>
        <artifactId>jakarta.mail-api</artifactId>
        <version>2.1.2</version>
    </dependency>
    
  4. Rebuild and test: After making the necessary changes to your dependencies, rebuild and test your application to ensure that the issue is resolved and the Jakarta Mail API is functioning correctly.

Video Explanation:

The following video, titled "Related Video", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

... development of Spring Boot projects. In this video, I will show you why that exception is thrown, the cause of the error, and solutions to fix.