How to send email from Liferay Portlet
Often there is the need of having to send email from your application. Each development platform provides its own tools to perform this task and the Java platform provides this capability natively through the JavaMail framework.
From Liferay platform (Java based) is obviously possible (“with no prohibition”) using JavaMail to send email from the portlet. In general, my idea is that using the tools offered by the development platform, is the best way to go. To send email notifications, Liferay provides the Message Bus, avoiding the direct use of the JavaMail API making it easier operation.
There are two destinations configured on the Message Bus that allow you to send email:
- liferay/subscription_sender: on this target “answers” the listener SubscriptionSenderMessageListener
- liferay/mail: on this target “answers” the listeneril listener MailMessageListener
Delivering your message to the Message Bus, on one of the above destinations, the message will be sent (asynchronously) via email. Sending emails considers the configuration of Liferay Mail (refer to the parameters SMTP).
Messages sent to both destinations are then sent as an email to the recipients, each destination has different responsibilities, in particular:
- the messages forwarded via liferay/subscription_sender all those messages are subjected to a “treatment” and that will be delivered to those who have subscribed to certain elements:
- Message Boards
- the messages forwarded via liferay/mail are generally simple messages that are sent by “direct route” via email
On the repository Liferay Portlet Example Subscribe Message Bus is available a complete portlet that shows the use of two different mechanisms for sending email. The sample portlet, send an email containing the list of users who are in off state.
Once again we have seen how the Liferay Message Bus can meet your needs in a simple and effective. The Message Bus service provider as “send email” avoids the proliferation of dozens of lines of code JavaMail.