javajndilogbacksmtpappender

Logback SMTPAppender: Where/how to specify to/from field when using JNDI


Edit: All I'm really asking here is: how do you specify to and from email addresses with Logback's SMTPAppender when it's configured to use JNDI lookups? This should be a basic form of functionality for the SMTPAppender and there'd be no way for SMTPAppender to work with JNDI lookups if it doesn't support this functionality!

I have the following Logback SMTPAppender defined:

<appender name="logManager-smtpAppender" class="ch.qos.logback.classic.net.SMTPAppender">
    <filter class="ch.qos.logback.classic.filter.LevelFilter">
        <level>WARN</level>
        <onMatch>ACCEPT</onMatch>
        <onMismatch>NEUTRAL</onMismatch>
    </filter>
    <filter class="ch.qos.logback.classic.filter.LevelFilter">
        <level>ERROR</level>
        <onMatch>ACCEPT</onMatch>
        <onMismatch>DENY</onMismatch>
    </filter>

    <asynchronousSending>false</asynchronousSending>

    <sessionViaJNDI>true</sessionViaJNDI>
    <jndiLocation>java:comp/env/mail/Session-local</jndiLocation>

    <subject>%logger{20} - %m</subject>
    <layout class="ch.qos.logback.classic.html.HTMLLayout"/>
    <cyclicBufferTracker class="ch.qos.logback.core.spi.CyclicBufferTracker">
        <bufferSize>25</bufferSize>
    </cyclicBufferTracker>
</appender>

As you see, I'm using JNDI to lookup mail server credentials.

When this runs, I get:

15:50:06,857 |-INFO in ch.qos.logback.classic.net.SMTPAppender[logManager-smtpAppender] - Empty destination address. Aborting email transmission

This made me realize: no where in the SMTPAppender above, or in my context.xml am I specifying the to/from email addresses.

How/where (examples, please!) do I specify these?!? I checked Logback's source code and this message gets printed from inside SMTPAppenderBase.java:

List<InternetAddress> destinationAddresses = parseAddress(lastEventObject);
if (destinationAddresses.isEmpty()) {
    addInfo("Empty destination address. Aborting email transmission");
    return;
}

private List<InternetAddress> parseAddress(E event) {
    int len = toPatternLayoutList.size();

    List<InternetAddress> iaList = new ArrayList<InternetAddress>();

    for (int i = 0; i < len; i++) {
        try {
            PatternLayoutBase<E> emailPL = toPatternLayoutList.get(i);
            String emailAdrr = emailPL.doLayout(event);
            if (emailAdrr == null || emailAdrr.length() == 0) {
                continue;
            }
            InternetAddress[] tmp = InternetAddress.parse(emailAdrr, true);
            iaList.addAll(Arrays.asList(tmp));
        } catch (AddressException e) {
            addError("Could not parse email address for [" +         toPatternLayoutList.get(i) + "] for event [" + event + "]", e);
            return iaList;
        }
    }
    return iaList;
}

But I still can't tell where/how I'm supposed to set to/from fields. Any ideas? Thanks in advance!


Solution

  • This is a known bug. SMTPAppender cannot use JNDI as a connection source and successfully send emails as of 1.0.13 (it was prematurely released).