user1016403
user1016403

Reputation: 12621

NoSuchProviderException: smtp with log4j SMTP appender

I am using log4j to send an email when there is an exception. below is my log4j properties file configuration.

log4j.rootLogger=WARN, R, email
log4j.appender.R=org.apache.log4j.ConsoleAppender
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d{HH:mm:ss} %-5p [%c{1}]: %m%n
log4j.appender.email=org.apache.log4j.net.SMTPAppender
log4j.appender.email.BufferSize=10
log4j.appender.email.SMTPHost=myhost.com
[email protected]
[email protected]
log4j.appender.email.Subject=Error
log4j.appender.email.layout=org.apache.log4j.PatternLayout

mine is maven project i have added dependencies for mail.jar, activation.jar and smtp.jar. But on application server startup itself i get below error:

[ERROR] log4j:ERROR Error occured while sending e-mail notification.
[ERROR] javax.mail.NoSuchProviderException: smtp
[ERROR]     at javax.mail.Session.getService(Session.java:782)
[ERROR]     at javax.mail.Session.getTransport(Session.java:708)
[ERROR]     at javax.mail.Session.getTransport(Session.java:651)
[ERROR]     at javax.mail.Session.getTransport(Session.java:631)
[ERROR]     at javax.mail.Session.getTransport(Session.java:686)
[ERROR]     at javax.mail.Transport.send0(Transport.java:166)

Am i missing any thing here? What is the root cause of the error? is it because of incorrect SMTP host name? or is it because of any missing/conflicting dependencies?

Upvotes: 1

Views: 1589

Answers (3)

jmehrens
jmehrens

Reputation: 11045

Upgrade your JavaMail to 1.5.3 which contains the fix for Bug 6668 -skip unusable Store and Transport classes. From the bug report:

In complex class loading situations, it can be possible for there to be multiple copies of the JavaMail classes. A Store or Transport defined by one copy may be loaded by another copy, but it won't be usable because they're in different ClassLoaders. In this case, JavaMail should skip over the unusable class and try to load the class from another ClassLoader.

This can happen, for example, in GlassFish if the application includes the JavaMail classes, the application class loader is configured to prefer application classes over system classes, and the app server itself tries to use JavaMail when running in the context of the application.

You can download the latest snapshot and official releases from the JavaMail reference implementation home page.

Upvotes: 1

MonoThreaded
MonoThreaded

Reputation: 12033

You probably want to export smtp.jar to be usable by your webapp.

Upvotes: 0

Bill Shannon
Bill Shannon

Reputation: 29971

You don't need smtp.jar and mail.jar - everything in smtp.jar is also in mail.jar. Get rid of smtp.jar, although I doubt that will solve your problem.

Also, make sure you don't have any other jar files with JavaMail classes in your classpath, such as javaee.jar or j2ee.jar.

This is most likely a classpath problem of some sort. JavaMail uses the class loader to find the configuration file that configures the providers such as "smtp". If the class loader handles resource lookups incorrectly, this problem can occur.

Upvotes: 1

Related Questions