【问题标题】:Unable to listen to events using Apache Commons Email无法使用 Apache Commons 电子邮件收听事件
【发布时间】:2013-08-10 22:50:46
【问题描述】:

我正在使用 Apache 公共电子邮件库发送电子邮件。

但是,我无法收听 Connection 和 Transport 事件。我使用以下方法添加了事件侦听器:

email.getMailSession().getTransport().addConnectionListener(this);
email.getMailSession().getTransport().addTransportListener(this);

...但没有收到任何事件。

我的代码如下:

public class MailSendTest implements ConnectionListener, TransportListener{
final Email email = new SimpleEmail();

public void sendEmail(){
    try {
        email.setHostName("smtp.host.com");
        email.setFrom("from@host.com");
        email.addTo("to@host.com");
        email.setBounceAddress("from@host.com");
        email.setSubject("Testing");
        email.setMsg("Test Message");
        email.setDebug(true);
        email.setAuthentication("from@host.com", "pass");
        email.setSslSmtpPort("465");
        email.setSocketTimeout(60000);

        email.getMailSession().getTransport().addConnectionListener(this);
        email.getMailSession().getTransport().addTransportListener(this);

        email.send();

    } catch (Exception ex) {
        JOptionPane.showMessageDialog(null, "Err : "+ex.getMessage());
    } 
}


@Override
public void opened(ConnectionEvent e) {
    System.out.println("####Connected to "+ email.getHostName());
}

@Override
public void disconnected(ConnectionEvent e) {
    System.out.println("####Disconnected from "+ email.getHostName());
}

@Override...

}

任何帮助将不胜感激。

【问题讨论】:

    标签: apache-commons-email


    【解决方案1】:

    我也发现它无法这样做,但经过一些挖掘......我发现commons mail依赖于java mail API(mail.jar),所以apache commons mail只是抽象应该是什么如果您使用的是 java 邮件 API,请谨慎完成。

    我的建议是按照本教程here 的操作,即创建 Session 对象,然后从中检索 Transport 对象......然后您可以附加您的 TransportListener。

    我目前仍在做上述方式(在写这篇文章时),我希望它的工作! 顺便说一句,使用this 来定义 SMTP 的属性。

    祝你好运

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-16
      • 1970-01-01
      • 2010-10-25
      • 2011-09-17
      • 1970-01-01
      • 2018-09-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多