Tuesday, November 19, 2013

Send Mails in Liferay Plugin Portlet

Send Mails in Liferay Plugin Portlet


Objective:

Send mails in liferay  plug-in portlet.

Liferay provide inbuilt mail sending functionality from that we can send mails. Liferay provide simple administration screen for mail server configuration so that we can configure mails server and we can send mails in plug-in portlet development.

Download MailSender  portlet from following location

You can find source and war file


Note: 

Portlet developed in Liferay 6.1GA2 EE version
If you want deploy in CE version you just do changes in liferay-plugin-package.properties

Liferay 6.1 EE version

name=MailSender
module-group-id=liferay-ee
module-incremental-version=1
tags=
short-description=
change-log=
page-url=http://www.liferay.com
author=Liferay, Inc.
licenses=EE
portal-dependency-jars=\
    jstl-api.jar,\
    jstl-impl.jar
portal-dependency-tlds=c.tld
liferay-versions=6.1.20


Liferay 6.1 CE version

name = MailSender
module-group-id=liferay
module-incremental-version=1
tags=
short-description=
change-log=
page-url=http://www.liferay.com
author=Liferay, Inc.
licenses=LGPL
portal-dependency-jars=\
    jstl-api.jar,\
    jstl-impl.jar
portal-dependency-tlds=c.tld

liferay-versions=6.1.1

Procedure for deploy portlet:

You can use war file and directly place in your portal deploy folder and test or you can also use source to deploy portlet.
Once portlet is deployed successfully you can see the portlet in sample category name as Mail Sender.

The following are the steps to send mails in liferay

  • Configure mail server
  • Use Liferay MailServieUtil to send mails


Configure mail server

We can configure mail configuration in liferay server administration part.

The mail configuration nothing but specify SMTP and IMAP/POP details.

SMPT: SMTP (Simple Mail Transfer Protocol) used to send mails

POP/IMAP: This is used to receive mails.

Note:

For every mail server we will have this information so that we can configure in liferay so that we can send and receive mails.

Example Mail servers:  JAMES mail server, Zimbra

Available Mail server


If we don’t have any mail server we can use our GMAIL as mail server in liferay. Gmail also provides SMTP and POP/IMAP details so that we can use GMAIL server information for send and receive mails in liferay.

The following are the GMAIL server setting information URL


Note:

In the following mail configuration I have used GMAIL. So you can also use your GMAIL.
Here only change is you have provide your GMAIL email address and password .

Liferay mail configuration:

Login as Liferay Admin and go to control panel.

In right side panel go to Server section and click Server Administration

Select mail tab and fill with your mail server information

The following is example screen for mail configuration in liferay




Note:

You can use IMAP or POP as incoming server.

For Gmail mail server setting go through following link


Based on your secure layer implementation for you server you have to change port number to SMTP

Like if you use SSL then SMTP port is 465 or if you are use TLS then SMTP port is 587

Use Liferay  MailServieUtil to send mails

Liferay provide some of mail implementation java classes in liferay kernel level so that we can use in plug-in portlet. We have one of util class called MailServieUtil by using this we can send mails.

MailServiceUtil.sendEmail(mailMessage)  this method take MailMessage object.


Note:

MailMessage having much setter methods to format mail message.


The following is full code to send mail



public class MailSenderAction extends MVCPortlet {
            public void sendMailMessage(
                                    ActionRequest actionRequest, ActionResponse actionResponse)
                        throws IOException, PortletException, AddressException {
                        System.out.println("====sendMailMessage===");
                        String mailSubject=ParamUtil.getString(actionRequest,"mailSubject");
                        String mailBody=ParamUtil.getString(actionRequest,"editor");
                        String senderMailAddress=ParamUtil.getString(actionRequest,"senderEmailAddess");
                        String receiverMailAddress=ParamUtil.getString(actionRequest,"receiverEmailAddess");
                        System.out.println("1111"+mailBody);
                        try {
                                    MailMessage mailMessage=new MailMessage();
                    mailMessage.setBody(mailBody);
                    mailMessage.setSubject(mailSubject);
                    mailMessage.setFrom(new InternetAddress(senderMailAddress));
                    mailMessage.setTo(new InternetAddress(receiverMailAddress));
                                    MailServiceUtil.sendEmail(mailMessage);
                             SessionMessages.add(actionRequest.getPortletSession(),"mail-send-success");
                        }
                        catch (Exception e) {
                            e.printStackTrace();
                        }
            }


Screens:




Author
Meera Prince

1 comment :

Recent Posts

Recent Posts Widget

Popular Posts