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.. :)

Friday 9 November 2012

Generate .xsd Schema file from .xml file

Create a Scheduler in Liferay
An easy way to generate your .xsd file from .xml is with trang command line tool.

Below are the steps to generate .xsd file from .xml file.

  1.     Download tarng.jar file and save it at your local drive.  
  2.       Generate .xsd file using below command
  • java -jar <<your local machine path of trang.jar>>trang.jar addToCart.xml addToCart.xsd

E.g suppose your .xml file is as below then It will generate .xsd

addToCart.xml

<?xml version="1.0" encoding="UTF-8"?>
<addToCart> 
         <cartItem>   
             <cart_item_id><![CDATA[156]]></cart_item_id> 
             <cart_id><![CDATA[9]]></cart_id>
             <nid><![CDATA[167]]></nid>   
             <qty><![CDATA[1]]></qty>   
             <title><![CDATA[Born To Die (Album)]]></title>
             <price><![CDATA[99]]></price>
             <discountedPrice><![CDATA[89.1]]></discountedPrice>
         </cartItem>  
 </addToCart>

addToCart.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
  <xs:element name="addToCart">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="cartItem"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="cartItem">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="cart_item_id"/>
        <xs:element ref="cart_id"/>
        <xs:element ref="nid"/>
        <xs:element ref="qty"/>
        <xs:element ref="title"/>
        <xs:element ref="price"/>
        <xs:element ref="discountedPrice"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="cart_item_id" type="xs:integer"/>
  <xs:element name="cart_id" type="xs:integer"/>
  <xs:element name="nid" type="xs:integer"/>
  <xs:element name="qty" type="xs:integer"/>
  <xs:element name="title" type="xs:string"/>
  <xs:element name="price" type="xs:integer"/>
  <xs:element name="discountedPrice" type="xs:decimal"/>
</xs:schema>

 



Tuesday 6 November 2012

Liferay remove docbar for general user

Liferay remove docbar for general user If you want to remove dockbar for general users form liferay page.
Add the below code in your portal_normal.vm file.


#if ( $is_signed_in )
#set ($rService = $serviceLocator.findService(“com.liferay.portal.service.RoleService”))
#set ($usrRoles = $rService.getUserRoles( $user_id ))
#set ($hasPermission = false)
#foreach( $usrRole in $usrRoles )
#if ( $usrRole.getName() == “Administrator” || $usrRole.getName() == “Content-Admin” || $usrRole.getName() == “Content-Editor”)
#set ($hasPermission = true)
#end
#end
#end
#if($hasPermission == true)
#dockbar()
#end