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.

Burabari
  • 63
  • 1
  • 5

1 Answers1

0

I'm also found it unable to do so, but after do some digging ... I found out that commons mail is dependent to java mail API (mail.jar), so apache commons mail is just abstracting what should be done discreetly if you are using java mail API.

What I suggest is to follow what this tutorial here do, which is creating Session object, then retrieves the Transport object from it ... then you can attach your TransportListener.

I am still doing the above way currently (while writing this), I hope its work! By the way, use this to define properties for SMTP.

Good luck

Bromo Programmer
  • 670
  • 2
  • 13
  • 37