Logging & Debugging

§

Enable javax.mail debug logging

By default javax.mail's debug logging is turned off, but you can turn it on for more diagnostic info. All this does is enabling debug mode on the internal Session instance.

Mailer mailer = new Mailer(...);
mailer.setDebug(true);

// or
mailer.getSession().setDebug(true);

// or
yourSession.setDebug(true);
Mailer mailer = new Mailer(yourSession);
§

Catching exceptions

Simple Java Mail throws a MailException if something goes wrong and logs these by default using SLF4J. This includes checked exceptions thrown by underlying frameworks. An exception might be thrown when:

  1. 1. sending an email
  2. 2. signing an email with DKIM
  3. 3. converting from MimeMessage to Email
Email email = new Email();

try {
   new Mailer().sendMail(email);
} catch (MailException e) {
   // handle the exception (but it is already logged through SLF4J)
}
§

Logging output

Simple Java Mail uses SLF4J to log, which means you can use any logging framework you like that supports it. Also read this excellent summary of how to configure slf4j with log4j2.

There are three parts of Simple Java Mail that may produce logging: "org.simplejavamail", "socks5bridge" and "org.simplejavamail.mailer.internal.socks". The latter two are for the proxy bridge that is used for authenticated proxy connections .

If you wish to know where and how the properties are loaded, you can increase logging for "org.simplejavamail.internal.util.ConfigLoader". This will tell you which properties are loaded by API and which from config file and which were properties are without values.

When using maven, Simple Java Mail provides some default XML configuration for log4j2, but you can easily switch to something else. Below are some examples.

§

Example with log4j12

Here's an example configuration for log4j12, make sure you have the slf4j-to-log4j impl included on the classpath as well as log4j12 itself and the log4j.xml with the config.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">

    <appender name="console" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d [%t] %-5p %c{1} - %m%n"/>
        </layout>
    </appender>
    <appender name="simpleConsole" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d Simple Java Mail SOCKS5 bridge - %p %m%n"/>
        </layout>
    </appender>

    <logger name="org.simplejavamail">
        <level value="trace"/>
        <appender-ref ref="console"/>
    </logger
    <!-- in case you're using authenticated proxy -->
    <logger name="socks5bridge" additivity="false">
        <level value="info"/>
        <appender-ref ref="simpleConsole"/>
    </logger>
    <logger name="org.simplejavamail.mailer.internal.socks">
        <level value="warn"/>
    </logger>

</log4j:configuration>
§

Example with log4j2

Here's an example configuration for log4j2, make sure you have the log4j-slf4j-impl impl included on the classpath as well as log4j2 itself and the log4j2.xml with the config.

<?xml version="1.0" encoding="UTF-8"?>
<configuration status="OFF">
    <appenders>
        <Console name="console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss} [%t] %-5level %c{1} - %msg%n" />
        </Console>
        <Console name="simpleConsole" target="SYSTEM_OUT">
            <PatternLayout pattern="%d Simple Java Mail SOCKS5 bridge - %level %m%n" />
        </Console>
    </appenders>
    <Loggers>
        <Logger name="org.simplejavamail" level="trace"/>
        <!-- in case you're using authenticated proxy -->
        <Logger name="socks5bridge" level="info" additivity="false">
            <AppenderRef ref="simpleConsole" />
        </Logger>
        <Logger name="org.simplejavamail.mailer.internal.socks" level="warn"/>

        <Root level="warn">
            <AppenderRef ref="console" />
        </Root>
    </Loggers>
</configuration>
§

Example with logback

Here's an example configuration for logback, make sure you have the logback-classic included on the classpath as well as logback-core and the logback.xml with the config.

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %c{1} - %msg%n</pattern>
        </encoder>
    </appender>
    <appender name="simpleConsole" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d Simple Java Mail SOCKS5 bridge - %level %m%n</pattern>
        </encoder>
    </appender>

    <logger name="org.simplejavamail" level="TRACE"/>
    <!-- in case you're using authenticated proxy -->
    <logger name="socks5bridge" level="INFO" additivity="false">
        <appender-ref ref="simpleConsole" />
    </logger>
    <logger name="org.simplejavamail.mailer.internal.socks" level="WARN"/>

    <root level="WARN">
        <appender-ref ref="console" />
    </root>
</configuration>