我正在使用Apache commons email lib发送电子邮件。
但是,我无法收听连接和传输事件。我使用以下方法添加了事件监听器:
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...
}
任何帮助都将不胜感激。