13

I'm trying to set up my Java project to be able to send e-mail (via g-mail, if it matters) and am getting "javax.mail.NoSuchProviderException: No provider for smtps" every time I try to run the following line (which is copy/paste from their example).

Transport transport = session.getTransport("smtps");

I've looked around and found that this is generally thrown because you don't have the mail.jar included in your classpath, but I do in fact have the mail.jar included. Since I am running JDK 1.6 I do not need to include the activation.jar according to the FAQ here (http://www.oracle.com/technetwork/java/javamail/faq-135477.html#classpath). Further, the activation.jar does not seem to be present in version 1.4.7 of javamail.

Just in case something got corrupted, I re-downloaded the entire zip from oracle's website, extracted it and added the jar fresh (after deleting the old jar) and I am still getting the same error. Any thoughts as to what the issue could be at this point?

EDIT: Here is the full stack trace that is being printed:

javax.mail.NoSuchProviderException: No provider for smtps
    at javax.mail.Session.getProvider(Session.java:433)
    at javax.mail.Session.getTransport(Session.java:627)
    at javax.mail.Session.getTransport(Session.java:608)
... my code that calls getTransport() ...
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
... more of my code ...
    at java.lang.Thread.run(Thread.java:662)
JuniorIncanter
  • 1,569
  • 2
  • 16
  • 27

9 Answers9

10

Just in case anyone makes the same mistake as I have: you have to use lowercase letters for the protocol resolving to work. If you type SMTP as a protocol name instead of smtp you will get the NoSuchProviderException. It most likely works the same way for all other providers.

Dariusz
  • 21,561
  • 9
  • 74
  • 114
8

When you use Mail API make sure that which protocol are you expecting to work with? In this case you are missing smtp.jar with your eclipse project.

there are several jars for different protocols are available in mail api. EX: dsn.jar , gimap.jar , imap.jar ,mailapi.jar ,pop3.jar ,smtp.jar

nimesh makwana
  • 121
  • 1
  • 4
  • 2
    This is indeed the answer for many cases. Most of people will update javamail and will indeed work, but the problem presented is with version 1.4.7. The version 1.4.7 needs the smtp.jar. When you cannot update the javamail version, this is the solution! – nakib Jan 27 '15 at 19:34
7

So it turns out that the issue was that an outdated version of mail.jar was included in a project that I was referencing and, upon updating that copy of the mail.jar, the issue was resolved.

For future reference, is there any way to log or provide visibility on such jar conflicts?

https://confluence.atlassian.com/confkb/cannot-send-email-due-to-javax-mail-nosuchproviderexception-smtp-error-154079.html

JuniorIncanter
  • 1,569
  • 2
  • 16
  • 27
  • Thanks for this update. I had the same problem and same cause. My problem was solved by putting javax.mail.jar at the beginning of my CLASSPATH (I am in UNIX environment and am using command line). – Hampden123 Nov 22 '14 at 00:53
  • We experienced same situation using Java with Wildfly 8. We solved this issue including [smtp](http://mvnrepository.com/artifact/com.sun.mail/smtp/1.5.4) dependency on pom.xml to force provider to be loaded. Another solution which worked was configure mail on Wildfly administration and use @Resource. However latest approach demands a specific application server configuration. – Marcio Jasinski Oct 15 '15 at 17:25
4

Make sure that you have the javax.mail.jar in you build path. If you are using eclipse you may have to refresh or right click your project in the files explorer, select configure build path, add external JAR and then add it to the build path. Send email using java gives working code (I've tested it) in case you just want to look over yours. If that is not the problem, or you are not using eclipse, a stack trace would be nice

Community
  • 1
  • 1
Keith Aylwin
  • 516
  • 4
  • 15
  • I am using Eclipse. From my (limited) understanding, JDK 1.6 automatically includes the JAF. Is this not the case? – JuniorIncanter May 31 '13 at 18:55
  • I'm sorry I switched things around in my head when I wrote that. You need to make sure that the **javax.mail.jar** is in your build path.(use the same steps I wrote above). I just re-ran the e-mail code in the link above and it runs without the framework in my imports. – Keith Aylwin May 31 '13 at 22:59
  • It is in my build path. I've confirmed this by right-clicking on my project (In Eclipse), selecting "Properties", selecting "Java Build Path", and finally selecting "Libraries". I can see the mail.jar there. I can also see it under the "Order and Export" tab. I've added the stack trace in case that would help. – JuniorIncanter Jun 03 '13 at 19:19
  • Also, for reference, I've added the Cheok Yan Cheng's comment on that link you provided (http://stackoverflow.com/questions/3649014/send-email-using-java) and I get the same error on the same line. – JuniorIncanter Jun 03 '13 at 20:01
1

JavaMail asks the ClassLoader for the configuration file that configures the protocol providers. If the ClassLoader doesn't work correctly, JavaMail won't be able to find the configuration file. There's an incompatibility between the way the OSGi ClassLoaders work and what JavaMail expects, which can cause this problem. If you're running your application in Eclipse itself, that might explain this problem. Another common cause of this problem is importing the mail.jar file into your project in such a way that the class files are extracted from the jar file and included in your application, but the configuration files are left behind.

Try running your program from the command line using the "java" command and with the mail.jar file in your CLASSPATH.

Bill Shannon
  • 29,579
  • 6
  • 38
  • 40
  • Tried that, and thought it had some chance as that's fixed another issue, but no dice in this situation. – JuniorIncanter Jun 03 '13 at 19:20
  • But for reference, I am running the application in Eclipse itself. – JuniorIncanter Jun 03 '13 at 19:31
  • If you can, try running it outside of Eclipse. Does "smtp" fail in the same way? Try: session.addProvider(new Provider(Provider.Type.Transport, "smtps", "com.sun.mail.smtp.SMTPSSLTransport", "", "")); – Bill Shannon Jun 04 '13 at 00:02
1

for the benefit of others with a similar problem as I had.

make sure you remember to set

 properties.put("mail.transport.protocol", "smtp"));

instead of

 properties.put("mail.transport.protocol", "SMTP"));
virtuvious
  • 2,362
  • 2
  • 21
  • 22
0

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
0

I got the same issue. i resolved it by adding the correct dependency in maven. group id - javax.mail artifactory - mail version 1.4.7

if you are doing, without maven build, add the correct library for javax.mail

-2

try

Transport transport = session.getTransport("smtp");
0day
  • 103
  • 1
  • 10