Using 2 files :
1. signupservlet.java
2. Mail.java
_______________________________________
signupservlet.java
... _______________________________________
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
import java.util.Random;
public class signupservlet extends HttpServlet
{
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
{
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/snp","root","");
Statement st=con.createStatement();
String fname,lname,semail,spassword,sex,month;
int date,year;
fname=request.getParameter("fname");
lname=request.getParameter("lname");
semail=request.getParameter("semail");
spassword=request.getParameter("spassword");
sex=request.getParameter("sex");
date=Integer.parseInt(request.getParameter("date"));
month=request.getParameter("month");
year=Integer.parseInt(request.getParameter("year"));
Random randomGenerator = new Random();
int randomid=randomGenerator.nextInt(1000000);
String sqlc="insert into userinfo Values('"+fname+"','"+lname+"','"+semail+"','"+spassword+"','"+sex+"'," +date+ ",'"+month+"',"+year+",NULL,'"+randomid+"')";
st.executeUpdate(sqlc);
//handling email confirmation
Mail mail = new Mail();
month="";
month+= "http://localhost:8084/snc/signupconfirm.jsp"+"?randomid="+randomid+"&email="+semail;
Mail.sendMail("apnafb@gmail.com","mltfrmqnbsp","smtp.gmail.com","465","true","true",true,"javax.net.ssl.SSLSocketFactory","false",semail,"apnafb.com Email Conformation",month);
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("#html*");
out.println("#body*");
out.println("#center*#h1* Just One Step left...!! #/h1* #br/*");
out.println("#center*#h2* Go to your Email Where we have sent you a link..!! Click that link to verify you email...!!#/h2*#/center*");
out.flush();
}
catch(Exception e)
{
response.sendRedirect("/snc/index.jsp");
}
}
}
____________________________________________________
Mail.java
____________________________________________________
package servlet;
import java.util.*;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class Mail
{
public synchronized static boolean sendMail(String userName,String passWord,String host,String port,String starttls,String auth,boolean debug,String socketFactoryClass,String fallback,String to,String subject,String text)
{
Properties props = new Properties();
props.put("mail.smtp.user", userName);
props.put("mail.smtp.host", host);
if(!"".equals(port))
props.put("mail.smtp.port", port);
if(!"".equals(starttls))
props.put("mail.smtp.starttls.enable",starttls);
props.put("mail.smtp.auth", auth);
if(debug)
props.put("mail.smtp.debug", "true");
else
props.put("mail.smtp.debug", "false");
if(!"".equals(port))
props.put("mail.smtp.socketFactory.port", port);
if(!"".equals(socketFactoryClass))
props.put("mail.smtp.socketFactory.class",socketFactoryClass);
if(!"".equals(fallback))
props.put("mail.smtp.socketFactory.fallback", fallback);
try
{
Session session = Session.getDefaultInstance(props, null);
session.setDebug(debug);
MimeMessage msg = new MimeMessage(session);
msg.setText(text);
msg.setSubject(subject);
msg.setFrom(new InternetAddress("prembhaarti@gmail.com"));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
msg.saveChanges();
Transport transport = session.getTransport("smtp");
transport.connect(host, userName, passWord);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
return true;
}
catch (Exception mex)
{
mex.printStackTrace();
return false;
}
}
}
No comments:
Post a Comment