How to create an E-Mail Listener in Liferay6

An E-Mail Listener is done differently than a ModelListener I wrote about the last time. An E-Mail listener can´t be registered for an entity, it must be registered at the Liferay MessageBus. And this is how you do it:

Create the Listener Class

Implement the MessageListener class like this:

public class MailMessageListener implements MessageListener {

public void receive(Message arg0) throws MessageListenerException {
System.out.println("You got mail.");


Register the Listener

In your portlet, upon startup you can register the Mail Listener like this:

MailMessageListener listener = new MailMessageListener();

MessageBusUtil.registerMessageListener(DestinationNames.MAIL, listener);

That´s all you have to do. If you want to register your listener for other Destinations, take a look at the class com.liferay.portal.kernel.messaging.DestinationNames

If you liked this tutorial it would be very nice, if you could click on some of the google ads you see on the right side. It helps me run this block and motivates me ;)

If you have any questions, feel free to leave a comment.