Step 1: Create one portlet plugin project with name MailingConfiguration , inside this plugin create one portlet name MailConfig
Step 2: Paste below code in portlet view.jsp
<%
@page import="javax.portlet.PortletURL"%> <%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %> <%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %> <portlet:defineObjects /> <p style="border:1px solid green;">Mail Configuration</p> <% PortletURL mailingURL = renderResponse.createActionURL(); mailingURL.setParameter(actionRequest.ACTION_NAME, "mailing"); %> <aui:form method="post" action="<%=mailingURL.toString()%>"> <aui:input name="name" label="Name" value=""/> <aui:input name="email" label="Email" value=""/> <aui:button type="submit" value="SEND" /> </aui:form>Step 3: Paste below code in portlet controller class MailConfig.java
package com.test; import java.io.IOException; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; import javax.portlet.ActionRequest; import javax.portlet.ActionResponse; import javax.portlet.PortletException; import com.liferay.mail.service.MailServiceUtil; import com.liferay.portal.kernel.mail.MailMessage; import com.liferay.portal.kernel.util.ParamUtil; import com.liferay.util.bridges.mvc.MVCPortlet; /** * Portlet implementation class MailConfig */ public class MailConfig extends MVCPortlet { public void mailing(ActionRequest actionRequest, ActionResponse actionResponse) throws IOException, PortletException { String name = ParamUtil.getString(actionRequest, "name"); String email = ParamUtil.getString(actionRequest, "email"); MailMessage mailMessage = new MailMessage(); try { mailMessage.setFrom(new InternetAddress("azharuddinmd864@gmail.com")); } catch (AddressException e) { // TODO Auto-generated catch block e.printStackTrace(); } mailMessage.setSubject("USER REGISTRATION"); mailMessage.setHTMLFormat(true); try { mailMessage.setTo(new InternetAddress(email)); } catch (AddressException e) { // TODO Auto-generated catch block e.printStackTrace(); } mailMessage.setBody("<html><body><table style='font-size:20px;color:black;width:100%;'>" + "<tr><td style='font-size:20px;font-weight:bold;color:black;padding:1%;'>"+name+" </td>"+ "</tr>" + "<tr>" + "<td style='font-size:16px;color:black;padding:1%;'>Thank you for registering</td>"+ "</tr>" + "</table></body></html>"); MailServiceUtil.sendEmail(mailMessage); } }Step 4: For email configuration we can use any on below ways:
i>Using admin control panel
ii>Using portal-setup-wizard.properties , we can define email configuration in this file also.
iii> Using portal-ext.properties
Paste below mailing configuration in portal-ext.properties file , after adding configuration restart server.
mail.session.mail.pop3.host=pop.gmail.com
mail.session.mail.pop3.password=PASSWORD
mail.session.mail.pop3.port=110
mail.session.mail.pop3.user=USER
mail.session.mail.imap.host=imap.gmail.com
mail.session.mail.imap.port=993
mail.session.mail.store.protocol=imap
mail.session.mail.transport.protocol=smtp
mail.session.mail.smtp.host=smtp.gmail.com
mail.session.mail.smtp.password= ur gmail account password.
mail.session.mail.smtp.user=azharuddinmd864@gmail.com
mail.session.mail.smtp.port=465
mail.session.mail.smtp.auth=true
mail.session.mail.smtp.starttls.enable=true
mail.session.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
Step 5: You have to Turn On your gmail account less secure app setting by going to below url
https://www.google.com/settings/security/lesssecureapps
It worked for me.. thanks
ReplyDeleteyea first one worked for me but portlet-ext.property not worked for me.......thanks...
ReplyDeleteUseful post keep posting
ReplyDeleteYou can check sending Mail by Using Tempelates for sending mail in proper format
ReplyDelete