Hello Guys
Here, I am writing blog for email verification with token. When user add additional email address it's send autocratically verification link to given email address.
Follow below steps to develop custom portlet for additional email verification.
Step 1: Create service.xml and put below code.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 6.2.0//EN" "http://www.liferay.com/dtd/liferay-service-builder_6_2_0.dtd">
<service-builder package-path="com.bhagwan.service" auto-namespace-tables="false">
<author>Bhagwan</author>
<namespace>Bhagwan</namespace>
<entity name="AdditionalEmailAddress" local-service="true" remote-service="true" cache-enabled="true">
<!-- PK fields -->
<column name="additionalEmailAddressId" type="long" primary="true" id-type="identity" />
<!-- Audit fields -->
<column name="additionalEmail" type="String" />
<column name="additionalEmailtoken" type="String" />
<column name="emailVerified" type="boolean" />
<column name="userId" type="long" />
<column name="createdDate" type="Date" />
<column name="modifiedDate" type="Date" />
<!-- Order -->
<order by="asc">
<order-column name="additionalEmailAddressId" />
</order>
<!-- Finder methods -->
<finder name="UserId" return-type="Collection">
<finder-column name="userId" />
</finder>
</entity>
</service-builder>
NOTE : Please build service.xml.
Step 2: Add below method in AdditionalEmailAddressLocalServiceImpl.java
public List<AdditionalEmailAddress> findByUserId(long userId) throws SystemException
{
return AdditionalEmailAddressUtil.findByUserId(userId);
}
Step 3: Create AdditionalEmail.java and put below code
package com.bhagwan.emailverify;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.mail.internet.InternetAddress;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import com.bhagwan.service.model.AdditionalEmailAddress;
import com.bhagwan.service.model.impl.AdditionalEmailAddressImpl;
import com.bhagwan.service.service.AdditionalEmailAddressLocalServiceUtil;
import com.liferay.mail.service.MailServiceUtil;
import com.liferay.portal.kernel.mail.MailMessage;
import com.liferay.portal.kernel.servlet.SessionErrors;
import com.liferay.portal.kernel.servlet.SessionMessages;
import com.liferay.portal.kernel.util.Base64;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.service.UserLocalServiceUtil;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.util.bridges.mvc.MVCPortlet;
public class AdditionalEmail extends MVCPortlet {
public void sendVerification(ActionRequest actionRequest, ActionResponse actionResponse)
{
String email=(String)actionRequest.getParameter("email").trim();
String verifyUrl=(String)actionRequest.getParameter("verifyUrl").trim();
ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime());
String encodedToken = Base64.encode((timeStamp+"email"+email+"email_"+themeDisplay.getUserId()).getBytes());
if (sendEmail(email,encodedToken, verifyUrl, themeDisplay)) {
SessionMessages.add(actionRequest, "request_processed",
"You have sent verification to \"" + email
+ "\" successfully.");
} else// error
{
SessionErrors.add(actionRequest, "problem-occurred");
}
}
public boolean sendEmail(String email,String encodedToken,String verifyUrl,ThemeDisplay themeDisplay)
{
try {
System.out.println(email+" "+encodedToken+" "+verifyUrl);
AdditionalEmailAddress savedata=new AdditionalEmailAddressImpl();
savedata.setAdditionalEmail(email);
savedata.setUserId(themeDisplay.getUser().getUserId());
savedata.setAdditionalEmailtoken(encodedToken);
savedata.setCreatedDate(new Date());
savedata.setEmailVerified(false);
AdditionalEmailAddressLocalServiceUtil.addAdditionalEmailAddress(savedata);
InternetAddress toAddress = new InternetAddress(email);
InternetAddress fromAddress = new InternetAddress(themeDisplay
.getCompany().getEmailAddress(), "bhagwan");
String body = "Hi<br><br> Please Verify your email address<br> Please click on below link : <br><a href='"
+ verifyUrl
+ "?token="+encodedToken+"'>VERIFY</a><br><br><b>Thanks</b><br>"
+ UserLocalServiceUtil.getUser(themeDisplay.getUserId())
.getFullName();
MailMessage mailMessage = new MailMessage();
mailMessage.setTo(toAddress);
mailMessage.setFrom(fromAddress);
mailMessage.setSubject("Email Verification "
+ email);
mailMessage.setBody(body);
mailMessage.setHTMLFormat(true);
MailServiceUtil.sendEmail(mailMessage); // Sending message
} catch (Exception e) {
return false;
}
return true;
}
}
Step 4 : Put below code in view.jsp
<%@page import="com.liferay.portal.kernel.portlet.LiferayWindowState"%>
<%@page import="com.liferay.portal.kernel.oauth.Token"%>
<%@page import="com.bhagwan.service.service.AdditionalEmailAddressLocalServiceUtil"%>
<%@page import="com.bhagwan.service.model.AdditionalEmailAddress"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@page import="com.liferay.portal.kernel.util.WebKeys"%>
<%@page import="com.liferay.portal.theme.ThemeDisplay" %>
<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui"%>
<%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme"%>
<%@ taglib prefix="liferay-portlet" uri="http://liferay.com/tld/portlet" %>
<liferay-theme:defineObjects />
<portlet:defineObjects />
<liferay-portlet:actionURL var="sendVerification" name="sendVerification">
</liferay-portlet:actionURL>
<portlet:renderURL var="verifyURL"
windowState="<%= LiferayWindowState.MAXIMIZED.toString() %>">
<portlet:param name="mvcPath" value="/verifyEmail.jsp" />
</portlet:renderURL>
<aui:form action="<%=sendVerification.toString() %>" method="post">
<aui:input name="email" type="text" label="" value="" id="email"></aui:input>
<aui:input name="verifyUrl" type="hidden" label="" value="<%=verifyURL.toString()%>" id="verifyUrl"></aui:input>
<aui:button type="submit" label="" value="send" ></aui:button>
</aui:form>
Step 5: Create verifyEmail.jsp and put below code.
<%@page import="com.bhagwan.service.model.impl.AdditionalEmailAddressImpl"%>
<%@page import="com.liferay.portal.kernel.util.StringPool"%>
<%@page import="java.util.List"%>
<%@page import="com.liferay.portal.kernel.util.Base64"%>
<%@page import="com.liferay.portal.kernel.portlet.LiferayWindowState"%>
<%@page import="com.liferay.portal.kernel.oauth.Token"%>
<%@page import="com.bhagwan.service.service.AdditionalEmailAddressLocalServiceUtil"%>
<%@page import="com.bhagwan.service.model.AdditionalEmailAddress"%>
<%@page import="com.liferay.portal.kernel.util.StringPool"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@page import="com.liferay.portal.kernel.util.WebKeys"%>
<%@page import="com.liferay.portal.theme.ThemeDisplay" %>
<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui"%>
<%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme"%>
<%@ taglib prefix="liferay-portlet" uri="http://liferay.com/tld/portlet" %>
<liferay-theme:defineObjects />
<portlet:defineObjects />
Hello <%=themeDisplay.getUser().getFullName() %><br>
<%
String emailToken = (String) request.getParameter("token").trim();
if (emailToken == null || emailToken.equalsIgnoreCase("null")
|| emailToken.length() == 0) {
%>
SORRY Your email is not Verify.
<%
} else {
String decodetoken = new String(Base64.decode(emailToken),
StringPool.UTF8);
String email = decodetoken.substring(
decodetoken.indexOf("email") + "email".length(),
decodetoken.indexOf("email_"));
System.out.println(email + " decodetoken " + decodetoken);
long userId = Long.parseLong(decodetoken.substring(decodetoken
.lastIndexOf("_") + 1));
System.out.println(" userId " + userId);
if (themeDisplay.getUser().getUserId() == userId) {
System.out
.println(" themeDisplay.getUser().getUserId() "
+ themeDisplay.getUser().getUserId());
List<AdditionalEmailAddress> additionalEmailAddresses = AdditionalEmailAddressLocalServiceUtil
.findByUserId(userId);
for (AdditionalEmailAddress additionalEmailAddress : additionalEmailAddresses) {
if (additionalEmailAddress.getAdditionalEmailtoken().equals(emailToken));
{
if (additionalEmailAddress.getEmailVerified() == false
&& additionalEmailAddress
.getAdditionalEmail()
.equalsIgnoreCase(email)) {
additionalEmailAddress.setEmailVerified(true);
AdditionalEmailAddressLocalServiceUtil
.updateAdditionalEmailAddress(additionalEmailAddress);
out.println("Your email : "
+ additionalEmailAddress
.getAdditionalEmail()
+ " has been verify successfully.");
} else {
out.println("Your email : "
+ additionalEmailAddress
.getAdditionalEmail()
+ " already verified.");
}
}
}
} else {
%>
SORRY Your email is not Verify.
<%
}
}
%>
1 Comment(s)