7

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.

Leo
  • 6,480
  • 4
  • 37
  • 52
amit
  • 181
  • 2
  • 4
  • 20
  • Make sure you're using the same versions of jar files: http://stackoverflow.com/questions/16861553/javax-mail-nosuchproviderexception-no-provider-for-smtps – tjg184 Oct 08 '14 at 13:42
  • you have to configure smtp in your tomcat server.xml, did you do that? – user2504380 Oct 08 '14 at 13:42
  • maybe tomcat already has its own javax.mail jar (geronimo-javamail jar) and it's conflicting with yours – Leo Oct 08 '14 at 13:43
  • 1
    I got the issue. Before jumping to solution, let me explain development environment I have. I have a 'desktop' java application which has auto email functionality and works fine. Mail.jar and SMTP.jar and added to this appn. Now I have plugged in the same desktop.jar with new web application which is deployed on Tomcat. This web appn is just a web interface which internally uses functionality from jar. But auto email wasn't working and throwing NoSuchProviderException. **Solution**: I removed auto email functionality from desktop appn and added to web appn. That's it! and it's working now. – amit Oct 09 '14 at 09:10
  • @user2504380 Configure it in server.xml how? How come my webapp has been working for six years without doing that, if it's essential? – user207421 Nov 15 '15 at 18:52

3 Answers3

6

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.** {*;}
Damian Kozlak
  • 7,065
  • 10
  • 45
  • 51
user1928058
  • 61
  • 1
  • 3
2

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)

Sumit Sundriyal
  • 815
  • 1
  • 11
  • 14
1

I encountered this issue in an Android project that I work on after introducing AWS SNS into the project. It turned out that the aws-android-sdk-core-2.2.20.jar library includes a file called javamail.providers in its META-INF folder.

Once I removed this file from the .jar this exception went away and I was able to send email via java mail once again.