TransportListener Example

In this post under Java Mail, I will explain with example the purpose and how to use TransportListener interface.

Whenever message is sent by calling “sendMessage” on Transport class in Java Mail Api, the below events are generated

1) MESSAGE_DELIVERED
2) MESSAGE_NOT_DELIVERED
3) MESSAGE_PARTIALLY_DELIVERED

Whenever these events are generated, the Java Mail API provide hooks in TransportListener interface, where we can perform certain actions. The hooks are as shown below.

1) public void messageDelivered(TransportEvent transportEvent)
2) public void messageNotDelivered(TransportEvent transportEvent)
3) public void messagePartiallyDelivered(TransportEvent transportEvent)

For our example, we will create a class which will implement the above interface as shown below

CustomTransportListener


package package3;

import javax.mail.event.TransportEvent;
import javax.mail.event.TransportListener;

public class CustomTransportListener implements TransportListener {
    @Override
    public void messageDelivered(TransportEvent transportEvent) {
        System.out.println("Message Delivery Successful");
    }

    @Override
    public void messageNotDelivered(TransportEvent transportEvent) {
        System.out.println("Message Delivery UnSuccessful");
    }

    @Override
    public void messagePartiallyDelivered(TransportEvent transportEvent) {
        System.out.println("Partial Message Delivered");
    }
}

Next we will register the custom TransportListener to the Transport class that sends the message as shown in the main code below

Main Class


1  package package3;
2  import java.util.Properties;
3  
4  import javax.mail.Address;
5  import javax.mail.Message;
6  import javax.mail.MessagingException;
7  import javax.mail.Session;
8  import javax.mail.Transport;
9  import javax.mail.internet.InternetAddress;
10 import javax.mail.internet.MimeMessage;
11 
12 public class Example3 {
13     public static void main(String[] args) {
14         Properties properties = new Properties();
15         Session session = Session.getInstance(properties);
16         session.setDebug(true);
17         MimeMessage mimeMessage = new MimeMessage(session);
18         
19         Transport t = null;
20         try {
21             CustomTransportListener customTransportAdapter = new CustomTransportListener();
22             t = session.getTransport("smtps");
23             t.addTransportListener(customTransportAdapter);
24             Address from = new InternetAddress("*****************@gmail.com");
25             Address to = new InternetAddress("***********@****.com");
26             
27             mimeMessage.setText("Welcome to email");
28             mimeMessage.setSubject("Welcome");
29             mimeMessage.setFrom(from);
30             mimeMessage.setRecipient(Message.RecipientType.TO, to);
31             
32             t.connect("smtp.gmail.com","*****************@gmail.com", "**************");
33             Address[] addresses = {to};
34             t.sendMessage(mimeMessage, addresses);
35         } catch(MessagingException excep) {
36             excep.printStackTrace();
37         } finally {
38             if(t != null) {
39                 try {
40                     t.close();
41                 } catch(MessagingException excep) {
42                     excep.printStackTrace();
43                 }
44             }
45         }
46     }
47 }

At line 21, we create an instance of CustomTransportListener named “customTransportAdapter”.

At line 22, we get an instance of Transport class that follows the SMTP protocol.

At line 23, we register the listener “customTransportAdapter” with the Transport instance by calling the “addTransportListener” method.

In this way we can use the TransportListener interface.

Leave a Reply