pls help me when i sent mail, it vil sending twice instead of once ,am using java.mail,am sending my code....
Every thing is working fine but it sending message twice instead of
once.
in the Forget.jsp page am accepting username or email
<struts-config.xml>
<form-beans>
<form-bean name="forgotUname"
type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="userName" type="java.lang.String" />
<form-property name="email" type="java.lang.String" />
</form-bean>
</form-beans>
<action-mappings>
<action path="/Forgot" name="forgotUname"
type="org.springframework.web.struts.DelegatingActionProxy"
parameter="operation"
input="/Forget.jsp" scope="request" validate="false">
<forward name="PasswordSuccess" path="/PasswordSuccess.jsp" />
<forward name="HomePage" path="/Forget.jsp"/>
<forward name="Index" path="/index.jsp" />
</action>
</action-mappings>
</struts-config.xml>
ForgotUnamePwdAction:
package com.infistech.eservices.apps.hr.actions;
import com.infistech.eservices.apps.hr.utils.Constants;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.validator.DynaValidatorForm;
import com.infistech.eservices.apps.hr.model.User;
import com.infistech.eservices.apps.hr.service.UserManager;
public class ForgotUnamePwdAction extends EservicesBaseAction {
private UserManager userManager;
public void setUserManager(UserManager userManager) {
this.userManager = userManager;
}
private static Logger log =
Logger.getLogger("ForgotUnamePwdAction.class");
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
DynaValidatorForm forgot = (DynaValidatorForm) form;
log.debug("In the execute mtd");
if (validationSuccessful(request, mapping, forgot)) {
String userName = forgot.getString("userName");
String toAddress = forgot.getString("email");
String password = null;
String uname = null;
String fromAddress = Constants.EMAILADDR_ORGANIZER;
if (toAddress == null && userName == null) {
ActionMessages msg = new ActionMessages();
ActionMessage msg1 = new ActionMessage("forgot.failed");
msg.add("message", msg1);
saveMessages(request, msg);
return mapping.findForward("HomePage");
}
if (toAddress != null && toAddress.equals("") != true) {
User user = null;
log.debug("EMAILID:" + toAddress);
try {
user = userManager.getUserByEmail(toAddress);
log.debug("USEROBJECT" + user);
if (user == null) {
ActionMessages msg = new ActionMessages();
ActionMessage msg1 = new ActionMessage("email.db");
msg.add("message", msg1);
saveMessages(request, msg);
return mapping.findForward("HomePage");
}
password = user.getPassword();
uname = user.getUserName();
String firstName = user.getFirstName();
String content = "Hi "
+ firstName
+ ",\n\n Here are your login details for your My Assurejobs
Account.\n\n UserName: "
+ uname + "\n Password: " + password
+ "\n\n\n Best Regards,\n AssureJobs Team";
log.debug("in first cond");
Properties props = new Properties();
props.put("mail.smtp.host", "192.168.1.2");
Session ses = Session.getDefaultInstance(props, null);
Message msg = new MimeMessage(ses);
msg.setFrom(new InternetAddress(fromAddress));
msg.setRecipients(Message.RecipientType.TO, InternetAddress
.parse(toAddress, false));
msg.setSubject("hi " + firstName
+ ", AssureJobs Login Details");
msg.setText(content);
Transport.send(msg);
return mapping.findForward("PasswordSuccess");
} catch (Exception e) {
ActionMessages msg = new ActionMessages();
ActionMessage msg1 = new ActionMessage("prob.db");
msg.add("message", msg1);
saveMessages(request, msg);
}
}
else if (userName != null && userName.equals("") != true) {
log.debug("INTHE IF ELSE CONDITION USERNAME" + userName);
log.debug("getting the user obj from Manager");
User user = null;
try {
user = userManager.getUserByUserName(userName);
if (user == null) {
ActionMessages msg = new ActionMessages();
ActionMessage msg1 = new ActionMessage("user.db");
msg.add("message", msg1);
saveMessages(request, msg);
return mapping.findForward("HomePage");
}
log.debug("USEROBJ" + user);
String password1 = user.getPassword();
String email = user.getEmail();
String firstName = user.getFirstName();
log.debug("FIRSTNAME:" + firstName);
String content = "Hi "
+ firstName
+ ",\n\n Here are your login details for your My Assurejobs
Account\n\n Password: "
+ password1
+ "\n\n\n Best Regards,\n AssureJobs Team";
Properties props = new Properties();
props.put("mail.smtp.host", "192.168.1.2");
Session ses = Session.getDefaultInstance(props, null);
Message msg = new MimeMessage(ses);
msg.setFrom(new InternetAddress(fromAddress));
msg.setRecipients(Message.RecipientType.TO, InternetAddress
.parse(email, false));
msg.setSubject("hi " + firstName
+ ", AssureJobs Login Details");
msg.setText(content);
Transport.send(msg);
return mapping.findForward("PasswordSuccess");
} catch (Exception e) {
log.debug("Unble to find the user with that username" + e);
ActionMessages msg = new ActionMessages();
ActionMessage msg1 = new ActionMessage("prob.db");
msg.add("message", msg1);
saveMessages(request, msg);
return mapping.findForward("HomePage");
}
}
ActionMessages msg = new ActionMessages();
ActionMessage msg1 = new ActionMessage("forgot.failed");
msg.add("message", msg1);
saveMessages(request, msg);
return mapping.findForward("HomePage");
}
else
return mapping.findForward("HomePage");
}
private boolean validationSuccessful(HttpServletRequest request,
ActionMapping mapping, ActionForm form)
{
ActionMessages errors = form.validate(mapping, request);
if (!errors.isEmpty()) {
saveErrors(request, errors);
if (log.isDebugEnabled())
log.debug("it return false");
return false;
} else {
if (log.isDebugEnabled())
log.debug("it returns true");
return true;
}
}
}
Here am using hibernate concepts for backend.
after sending mail it vil go to the success page.
every thing is working fine but it sending message twice instead of
once.
Thanks in Advance.
ShailajaD.