Search code examples
javaemaillogginglog4j2smtpappender

Log4J2 Factory Error when attempting to use SMTP Appender


Good afternoon,

When I attempt to use the SMTP Appender, I get an odd error in the console. The error appears to occur when the XML file is loaded, as it isn't logged via any output stream except stdout. The contents of the appender are as follows (and I have confirmed that the error is in this block of XML). I've removed our server information.

<SMTP name="Mailer">
    <Subject>[ERROR] (software name) on ${hostName} has thrown a fatal error</Subject>
    <To>(a valid email in the form of [email protected])</To>
    <From>(a valid email in the form of [email protected])</From>
    <SMTPHost>(The Internal IP address of a server in the form of 192.168.0.1)</SMTPHost>
    <SMTPPort>587</SMTPPort>
    <BufferSize>512</BufferSize>
</SMTP>

Whether or not it is referenced in a logger, I receive the following error immediately upon running the program and running getLogger from the log manager. I've removed a couple of the file names and replaced them with a rough description of what the file is doing at that point.

2015-05-19 19:08:18,812 ERROR Unable to invoke factory method in class class org.apache.logging.log4j.core.appender.SmtpAppender for element SMTP. java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.build(PluginBuilder.java:137) at org.apache.logging.log4j.core.config.AbstractConfiguration.createPluginObject(AbstractConfiguration.java:766) at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:706) at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:698) at org.apache.logging.log4j.core.config.AbstractConfiguration.doConfigure(AbstractConfiguration.java:358) at org.apache.logging.log4j.core.config.AbstractConfiguration.start(AbstractConfiguration.java:161) at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:361) at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:426) at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:442) at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:138) at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:147) at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:41) at org.apache.logging.log4j.LogManager.getContext(LogManager.java:175) at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:426) at (Our Log Interface Class, which essentially just returns the log passed by the Apache log manager) at (The global variable definition file, the first method to use getLogger) at (The main method for our software, where the globals are loaded) Caused by: java.lang.NoSuchMethodError: javax.mail.Session.setProtocolForAddress(Ljava/lang/String;Ljava/lang/String;)V at org.apache.logging.log4j.core.net.SmtpManager$SMTPManagerFactory.createManager(SmtpManager.java:325) at org.apache.logging.log4j.core.net.SmtpManager$SMTPManagerFactory.createManager(SmtpManager.java:299) at org.apache.logging.log4j.core.appender.AbstractManager.getManager(AbstractManager.java:71) at org.apache.logging.log4j.core.net.SmtpManager.getSMTPManager(SmtpManager.java:124) at org.apache.logging.log4j.core.appender.SmtpAppender.createAppender(SmtpAppender.java:142) ... 21 more

2015-05-19 19:08:18,814 ERROR Null object returned for SMTP in Appenders. 2015-05-19 19:08:18,819 ERROR Unable to locate appender Mailer for logger fatalerror

The details of the configuration are correct (I know it's a valid IP, etc) - they worked in log4j 1. The error log tells me virtually nothing about the error, so I am hoping someone has heard of this before. Thanks everyone!


Solution

  • This is the important line from your stacktrace:

    Caused by: java.lang.NoSuchMethodError: javax.mail.Session.setProtocolForAddress(Ljava/lang/String;Ljava/lang/String;)V

    The setProtocolForAddress method has been part of JavaMail since version 1.4, so this suggests to me that you are using an old version of that JAR. If you are, try upgrading to a later version.