Pages

Sunday 11 November 2012

Send an email from liferay portal

Create a Scheduler in Liferay

If you want to send an email from your liferay portal then its bit easy because liferay provide an Utility class called MailEngine.So we don't have to play with direct JavaMail API.

In this article, we’ll use Gmail as SMTP

Below are the steps to send an email in Liferay.
  •  Configure your SMTP provider, outgoing port, user id and password.
    • Login as super user in your liferay portal
    •   Go to Control Panel
    •  Click on Server Administration from server section
    • Click on Mail Tab
    • Fill out the SMTP fields here,
      1. Set Outgoing SMTP Server (E.g. smtp.gmail.com)
      2. Set Outgoing Port (E.g. 465)
      3. Check the box of Use a Secure Network Connection if you want.
      4. Set User Name and Password.
      5. Press “Save” Button 
      6. You are done with SMTP configuration of outgoing mail sever.

  • In your MVCPortlet or related class add the below peace of code to send an email.


MailMessage mailMessage = new MailMessage();
                   mailMessage.setHTMLFormat(true);
                   mailMessage.setBody("set body here");
                   mailMessage.setFrom(new InternetAddress("fromAddress","fromName"));
                   mailMessage.setSubject("set mail subject here");
                   mailMessage.setTo(new InternetAddress("set receiver email id here"));
                   mailEngine.send(mailMessage);
  • If you want to add attachment to your email or you want to add Cc you can use below send method of MailEngine class,

        send(InternetAddress from, InternetAddress[] to, InternetAddress[] cc, InternetAddress[] bcc, String subject, String body, boolean html format, InternetAddress[] replyTo, String messageId, String inReplyTo, File[] attachments);

I hope this tutorial will help you.. :)

1 comment:

  1. thanks ketan can u plz update a code for how to read the message from structure and template

    ReplyDelete