Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Simple mailing example in liferay portlets

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 936
    Comment on it

    This Blog will explore you how you can send the email using liferay portlets. The below code will helps you to send email using liferay portlet.

    Firstly you need to configure Liferay Mail Configuration:-

    Login using Liferay Admin account and go to control panel.

    then click on Configuration option on top panel.

    After click on Server Administration tab

    Select Mail tab and fill with your mail server information.

    NOTE:- In Name and Password field type your gmail account credentials.

    alt text

    Liferay provide java classes in liferay kernel level so that we use in portlet. We have MailServieUtil Class by using this we can send mails.

    view.jsp

    <%
    /**
     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
     *
     * This library is free software; you can redistribute it and/or modify it under
     * the terms of the GNU Lesser General Public License as published by the Free
     * Software Foundation; either version 2.1 of the License, or (at your option)
     * any later version.
     *
     * This library is distributed in the hope that it will be useful, but WITHOUT
     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
     * details.
     */
    %>
    
    
    <%@ taglib uri="http://java.sun.com/portlet&#95;2_0" prefix="portlet" %>
    <%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>
    <portlet:defineObjects />
    <portlet:actionURL name="mailSender" var="mailSender"></portlet:actionURL>
    
    
    
    <aui:form name="mailSender" id="mailSender" action="<%= mailSender.toString() %>">
        <aui:input type="text" name="toemail" fieldParam="toemail" />
        <aui:input type="textarea" name="subject" fieldParam="subject" />
    
        <aui:input type="submit" value="Submit" name="" />
    </aui:form>
    

    MailSender.java

    package com.evon;
    
    import javax.mail.internet.InternetAddress;
    import javax.portlet.ActionRequest;
    import javax.portlet.ActionResponse;
    import javax.portlet.ProcessAction;
    
    import com.liferay.mail.service.MailServiceUtil;
    import com.liferay.portal.kernel.mail.MailMessage;
    import com.liferay.portal.kernel.util.ParamUtil;
    import com.liferay.portal.kernel.util.WebKeys;
    import com.liferay.portal.theme.ThemeDisplay;
    import com.liferay.util.bridges.mvc.MVCPortlet;
    
    public class MailSender extends MVCPortlet {
    
        @ProcessAction(name="mailSender")
        public void mailSender(ActionRequest actionRequest,ActionResponse actionResponse)
                {
                        //String email,String subject,String emaibody
    
                        System.out.println("Inside mailSender    :");
                        System.out.println("---->"+ParamUtil.getString(actionRequest, "toemail"));
                        System.out.println("---->"+ParamUtil.getString(actionRequest, "subject"));
                        //System.out.println("---->"+ParamUtil.getString(actionRequest, "tomail"));
                    try {
                        ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME&#95;DISPLAY);
    
                        InternetAddress toAddress = new InternetAddress(ParamUtil.getString(actionRequest, "toemail"));
                        InternetAddress fromAddress = new InternetAddress(themeDisplay.getCompany().getEmailAddress(), "DentalNoteBox");
                        String body ="<br><br><br><b>Administrator</b><br><p>This is the Demo Example to Sending the mail</p><br><br>Thanx & Regards:-<br>Manish";
                        MailMessage mailMessage = new MailMessage();
                        mailMessage.setTo(toAddress);
                        mailMessage.setFrom(fromAddress);
                        mailMessage.setSubject(ParamUtil.getString(actionRequest, "subject"));
                        mailMessage.setBody(body);
                        mailMessage.setHTMLFormat(true);
                        MailServiceUtil.sendEmail(mailMessage); // Sending message
                    } catch (Exception e) {
                        e.printStackTrace();
    
                    }
    
                }
    
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: