java – Logback SMTPAppender:使用JNDI时在/从字段指定的位置/方式

编辑:我真正要问的是:当配置为使用JNDI查找时,如何使用Logback的SMTPAppender指定电子邮件地址和从电子邮件地址指定?这应该是SMTPAppender的基本功能形式,如果SMTPAppender不支持此功能,则无法使用JNDI查找!

我定义了以下Logback SMTPAppender:

<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>

如您所见,我正在使用JNDI查找邮件服务器凭据.

当这个运行时,我得到:

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

这让我意识到:上面的SMTPAppender中没有,或者在我的context.xml中,我指定了往返电子邮件地址.

我如何/在哪里(例子,请!)指定这些?!?我检查了Logback的源代码,这条消息从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;
}

但我仍然无法分辨我应该在哪里/如何设置/从字段.有任何想法吗?提前致谢!

解决方法:

这是一个已知的错误. SMTPAppender无法使用JNDI作为连接源,并且从1.0.13开始成功发送电子邮件(它过早发布).

上一篇:java-如何获取现有的JMS队列?


下一篇:javax.naming.NameNotFoundException:无法解析’MyDB’.已解决”Weblogic 12.1.3. C