0

I'm sending emails using Apache commons email lib.

However, I'm unable to listen to Connection and Transport events. I have added event listeners using:

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

...but don't receive any events.

My code is as follows:

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

}

Any help would be appreciated.

4

1 回答 1

0

我也发现它无法这样做,但是在进行了一些挖掘之后......我发现commons mail依赖于java mail API(mail.jar),所以apache commons mail只是抽象应该谨慎地做的事情,如果您正在使用 java 邮件 API。

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

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

祝你好运

于 2013-08-10T14:44:39.757 回答