Listening for events

Top  Previous  Next

For an object to listen for events published by the Smtp class the following steps are required:

 

1. Set object to implement SmtpListener or extend SmtpAdapter class.

2. Overload event handling methods.

3. Subscribe object to receive events published by Smtp instance.

 

The SmtpListener class is a pure interface and can be used in cases where the class you defined to listen for events already extends another class type. The SmtpAdapter class implements the SmtpListener interface and can be used in cases where there is no need for class inheritance.

 

Note

 

Unless your class requires inheritance it is generally easier to use the SmtpAdapter class as it provides default implementations for all the event handler methods defined in the SmtpListener interface. This allows you to overload only the event handler methods that you are interested in.

 

Example

 

The example below demonstrates using the SmtpListener class.

 

import com.jscape.inet.smtp.*;

 

public class MySmtpListener implements SmtpListener {

 

 public void connected(SmtpConnectedEvent event) {

         System.out.println("Connected to host: " + event.getHostname());

 }

 

 public void disconnected(SmtpDisconnectedEvent event) {

         System.out.println("Disconnected from host: " + event.getHostname());

 

 }

 

 public void commandSent(SmtpCommandEvent event) {

         System.out.println("Command sent: " + event.getCommand());

 

 }

 

 public void responseReceived(SmtpResponseEvent event) {

         System.out.println("Resonse received: " + event.getResponse());

 

 }

 

 public static void main(String[] args) {

         try {

                 Smtp smtp = new Smtp("smtp.myserver.com");

                 smtp.addSmtpListener(new MySmtpListener());

                 smtp.connect();

                 smtp.disconnect();

                 

         } catch(Exception e) {

                 e.printStackTrace();                        

         }

 }

 

}

 

Example

 

The example below demonstrates using the SmtpAdapter class.

 

import com.jscape.inet.smtp.*;

 

public class MySmtpAdapter extends SmtpAdapter {

 

 public void connected(SmtpConnectedEvent event) {

         System.out.println("Connected to host: " + event.getHostname());

 }

 

 public void disconnected(SmtpDisconnectedEvent event) {

         System.out.println("Disconnected from host: " + event.getHostname());

 }

 

 public static void main(String[] args) {

         try {

                 Smtp smtp = new Smtp("smtp.myserver.com");

                 smtp.addSmtpListener(new MySmtpAdapter());

                 smtp.connect();

                 smtp.disconnect();

                 

         } catch(Exception e) {

                 e.printStackTrace();                        

         }

 }

}

 

 

 





Home | Company | Products | Solutions | Purchase | Support | Services | Blog

© 2021 JSCAPE LLC