Log4j - SMTPAppender NoSuchProviderException smtp

时间:2013-02-11 12:26:22

标签: java error-handling log4j smtpappender

我正在尝试用log4j发送我的GWT项目的错误,但我遇到了问题。 这是我添加到log4j.xml文件中的内容:

<appender name="EMAIL" class="org.apache.log4j.net.SMTPAppender">
<param name="BufferSize" value="512" />
<param name="SMTPHost" value="localhost" />
<param name="From" value="myemail@teste.com" />
<param name="To" value="myemail@teste.com" />
<param name="Subject" value="[SMTPAppender] Application message" />
<layout class="org.apache.log4j.PatternLayout">
    <param name="ConversionPattern" value="%p %d (%c{1}) %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
    <param name="LevelMin" value="ERROR" />
    <param name="LevelMax" value="FATAL" />
</filter>
</appender>

<root>
<level value="WARN" />
<appender-ref ref="EMAIL" />
</root>

简单但在尝试发送电子邮件时出现以下错误?

log4j:ERROR Error occured while sending e-mail notification.
javax.mail.NoSuchProviderException: smtp
at javax.mail.Session.getService(Session.java:806)
at javax.mail.Session.getTransport(Session.java:728)
at javax.mail.Session.getTransport(Session.java:668)
at javax.mail.Session.getTransport(Session.java:648)
at javax.mail.Session.getTransport(Session.java:705)
at javax.mail.Transport.send0(Transport.java:192)
at javax.mail.Transport.send(Transport.java:124)
at org.apache.log4j.net.SMTPAppender.sendBuffer(SMTPAppender.java:330)
at org.apache.log4j.net.SMTPAppender.append(SMTPAppender.java:215)
at org.apache.log4j.AppenderSkeleton.doAppend(AppenderSkeleton.java:251)
at org.apache.log4j.helpers.AppenderAttachableImpl.appendLoopOnAppenders(AppenderAttachableImpl.java:66)
at org.apache.log4j.Category.callAppenders(Category.java:206)
at org.apache.log4j.Category.forcedLog(Category.java:391)
at org.apache.log4j.Category.error(Category.java:305)
at pt.keep.vault.web.server.WebServiceImpl.login(WebServiceImpl.java:1257)
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)
at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:561)
at com.google.gwt.user.server.rpc.RemoteServiceServlet.processCall(RemoteServiceServlet.java:208)
at com.google.gwt.user.server.rpc.RemoteServiceServlet.processPost(RemoteServiceServlet.java:248)
at com.google.gwt.user.server.rpc.AbstractRemoteServiceServlet.doPost(AbstractRemoteServiceServlet.java:62)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:487)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:362)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:729)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:405)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.handler.RequestLogHandler.handle(RequestLogHandler.java:49)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:324)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:505)
at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:843)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:647)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:211)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:380)
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:395)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:488)

我已经将activation-1.1.jar和mail.jar(1.4.6 API)添加到我的项目中。 可能有什么不对?

提前致谢。

1 个答案:

答案 0 :(得分:0)

试试这个配置 - 它对我有用,它使用gmail作为smtp服务器,在开发和生产环境中都可能是你的不错选择。我使用log4j版本:1.2.16和apache-log4j-extras版本1.0(仅当你使用FilterBasedTriggeringPolicy时才需要这个额外的lib)

<appender name="emailApp" class="org.apache.log4j.net.SMTPAppender">
    <param name="SMTPProtocol" value="smtps" /> 
    <param name="SMTPHost" value="smtp.gmail.com" />
    <param name="SMTPPort" value="465" />
    <param name="SMTPUsername" value="your.gmail.username" />
    <param name="SMTPPassword" value="xxxxx" />
    <param name="From" value="your.gmail.username@gmail.com" />
    <param name="To" value="target@devnull.com" />
    <param name="Subject" value="Logging from my application" />
    <param name="BufferSize" value="1" />
    <triggeringPolicy class="org.apache.log4j.rolling.FilterBasedTriggeringPolicy">
        <filter class="org.apache.log4j.varia.LevelRangeFilter">
            <param name="LevelMin" value="WARN"/>
            <param name="LevelMax" value="FATAL"/>
        </filter>
    </triggeringPolicy>
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{dd-MM-yyyy HH:mm:ss:SSS} %-4r %-5p %c %x - %m%n"/>
    </layout>
</appender>