In this post under Java Mail, I will explain the use of ConnectionListener interface with an example.
Note: For this example and other examples in future we need to have
1) Gmail account
2) GMail SMTP server address
3) Stop the antivirus software running on your local.
4) Create an application password. The steps are mentioned in the url https://support.google.com/accounts/answer/185833?p=InvalidSecondFactor
We can use ConnectionListener interface to listen for below events
1) CLOSED
2) DISCONNECTED
3) OPENED
These connection events are generated by Transport, Store, and Folder class implementations.
We can perform certain actions when these events are generated using the below hooks or methods provided in ConnectionListener interface.
1) void opened(ConnectionEvent e)
2) void disconnected(ConnectionEvent e)
3) void closed(ConnectionEvent e)
For our example we create a custom implementation of ConnectionListener interface named “CustomConnectionListener”.
CustomConnectionListener
package package4;
import javax.mail.event.ConnectionEvent;
import javax.mail.event.ConnectionListener;
public class CustomConnectionListener implements ConnectionListener {
@Override
public void closed(ConnectionEvent connectionEvent) {
System.out.println("Connection Successful");
}
@Override
public void disconnected(ConnectionEvent connectionEvent) {
System.out.println("Connection Disconnected");
}
@Override
public void opened(ConnectionEvent connectionEvent) {
System.out.println("Connection Opened");
}
}
As shown in the above code, we create a CustomConnectionListener class that implements ConnectionListener interface. We also provide implementations for the three interface methods.
Next we will show how to register with Transport class to listen for the Connection events
1 package package4;
2
3 import java.util.Properties;
4
5 import javax.mail.Address;
6 import javax.mail.Message;
7 import javax.mail.MessagingException;
8 import javax.mail.Session;
9 import javax.mail.Transport;
10 import javax.mail.internet.InternetAddress;
11 import javax.mail.internet.MimeMessage;
12
13 public class Example4 {
14 public static void main(String[] args) {
15 Properties properties = new Properties();
16 Session session = Session.getInstance(properties);
17 session.setDebug(true);
18 MimeMessage mimeMessage = new MimeMessage(session);
19
20 Transport t = null;
21 try {
22 CustomConnectionListener customConnectionListener = new CustomConnectionListener();
23 t = session.getTransport("smtps");
24 t.addConnectionListener(customConnectionListener);
25 Address from = new InternetAddress("**************@gmail.com");
26 Address to = new InternetAddress("**************@gmail.com");
27
28 mimeMessage.setText("Welcome to email");
29 mimeMessage.setSubject("Welcome");
30 mimeMessage.setFrom(from);
31 mimeMessage.setRecipient(Message.RecipientType.TO, to);
32
33 t.connect("smtp.gmail.com","**************@gmail.com", "**************");
34 Address[] addresses = {to};
35 t.sendMessage(mimeMessage, addresses);
36 } catch(MessagingException excep) {
37 excep.printStackTrace();
38 } finally {
39 if(t != null) {
40 try {
41 t.close();
42 } catch(MessagingException excep) {
43 excep.printStackTrace();
44 }
45 }
46 }
47 }
48 }
In the above main code, at line 22 we create an instance of CustomConnectionListener.
At line 24, we register the listener with the Transport implementation using the addConnectionListener method of Transport class.
In a similar way we can register a custom implemetation of ConnectionListener with Store and Folder class implementations.