Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring JavaMailSenderImpl javax.mail.NoSuchProviderException: smtp

I have a very large project with a ton of dependancies, and am getting the following exception when trying to send mail:

javax.mail.NoSuchProviderException: smtp

I know the code works because it is part of a library that is used in other projects. Does anyone know what would cause this exception to happen? I have looked through all of the jar's and the only one containing 'javax.mail' is 'mail-1.4.4-SNAPSHOT.jar'.

Is there some file/class that registeres the 'smtp' protocol, and it could be happening somewhere else in my classpath?

like image 299
wuntee Avatar asked Dec 06 '22 02:12

wuntee


2 Answers

It's been a while since this question has been asked but it's still worth answering :

What's happening is that your mail service is using the SMTP (usually, it is even considered as the default protocol to use)protocol to send mail. No matter what concrete class you're using as mail service (might depend on whether you're working with Java EE, Spring or something else), it ends up using classes from the javamail API. In this API, in order to establish a connection to your mail server, an instance of javax.mail.Session will be created and this object will dynamically load classes that provide support for the protocol to use.

When loading the required class, in case it is not found, the ClassNotFoundException is caught and re-threw as javax.mail.NoSuchProviderException, with a message indicating the missing protocol support.

The solution in that case is to add the smtp.jar to your classpath. With a Maven-based project, simply add the following dependency

    <dependency>
        <groupId>com.sun.mail</groupId>
        <artifactId>smtp</artifactId>
        <version>1.4.5</version>
    </dependency>
like image 182
kyiu Avatar answered Dec 31 '22 11:12

kyiu


I had this issue. Putting smtp.jar in classpath solved the issue.

like image 21
Rejeev Divakaran Avatar answered Dec 31 '22 12:12

Rejeev Divakaran