Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javax.mail.NoSuchProviderException: smtp

Tags:

java

email

Following is my code to send an email:

    Properties props = new Properties();
    props.put("mail.smtp.host", host);
    props.put("mail.debug", "false");

    final Session session = Session.getInstance(props);
    final Message msg = new MimeMessage(session);
    msg.setFrom(new InternetAddress(from));
    msg.setRecipients(Message.RecipientType.TO, toAddress);
    msg.setSubject(subject);
    msg.setSentDate(new Date());
    Multipart multipart = new MimeMultipart("related");
    BodyPart mbp1 = new MimeBodyPart();
    mbp1.setContent(body, "text/html");         
    multipart.addBodyPart(mbp1);
    Transport.send(msg);

Error Stack trace:

javax.mail.NoSuchProviderException: smtp
    at javax.mail.Session.getService(Session.java:764)
    at javax.mail.Session.getTransport(Session.java:689)
    at javax.mail.Session.getTransport(Session.java:632)
    at javax.mail.Session.getTransport(Session.java:612)
    at javax.mail.Session.getTransport(Session.java:667)
    at javax.mail.Transport.send0(Transport.java:154)
    at javax.mail.Transport.send(Transport.java:80)

Note:

  1. Same code works if executed as a desktop application. But throws above exception when deployed on tomcat.
  2. Latest mail.jar and smtp.jar are added to library.
  3. SMTP host address is also correct.

If someone can give me pointers it will be helpful.

like image 787
amit Avatar asked Oct 08 '14 13:10

amit


2 Answers

I also got into similar situation, but eventually could solve it. In my case issue was the proguard_rules.txt file where I needed to add:

-keep class javax.mail.** {*;}
-keep class javax.activation.** {*;}
-keep class com.sun.mail.dsn.** {*;}
-keep class com.sun.mail.handlers.** {*;}
-keep class com.sun.mail.smtp.** {*;}
-keep class com.sun.mail.util.** {*;}
like image 172
user1928058 Avatar answered Sep 19 '22 22:09

user1928058


I had mail.jar and activation.jar in tomcat's lib directory and mailapi.jar in application's lib directory. Application was reading mailapi.jar during runtime, since mailapi.jar is light weight version of mailing api and it needs smtp.jar that why application was throwing smtp exception. So, if you want to get rid of this exception,

Please resolve conflict between mail.jar and mailapi.jar by:

  • removing mailapi.jar (if it is there in the classpath)
  • OR just keep one pair of mailapi.jar and smtp.jar in the classpath and remove mail.jar
  • OR just keep one pair of mail.jar and activation.jar in the classpath (clean approach)

(FYI: I searched file system to find out mail related jar files and got to know that I have conflicting jar file in the following path (added by gradle in classpath) C:\workspace\.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\testapp\WEB-INF\lib)

like image 26
Sumit Sundriyal Avatar answered Sep 21 '22 22:09

Sumit Sundriyal