Sending Mail Through Servlet
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import Db.databse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author Naveen&Jyoti
*/
@WebServlet(urlPatterns = {"/Register.action"})
public class Register extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try
{
Date dateNow;
dateNow = new java.sql.Date(System.currentTimeMillis());
String ref=request.getParameter("ref");
String mailid=request.getParameter("mailid");
String repassword=request.getParameter("repassword");
databse db=new databse();
Connection con=db.connectit();
String sql="select * from register where mailid=?";
PreparedStatement pste=con.prepareStatement(sql);
pste.setString(1,request.getParameter("mailid"));
ResultSet rs=pste.executeQuery();
int count = 0;
out.println(""+request.getParameter("mailid"));
while(rs.next())
{
count = count + 1;
}
if(count > 1)
{
out.println("<script>alert('Emailid Already Exists!');window.location='newuser/index.jsp';</script>");
}
else if(count==0)
{
PreparedStatement pst=con.prepareStatement("insert into register(registerid,mailid,password,status,date) values(?,?,?,?,?)");
pst.setString(1, ref);
pst.setString(2, mailid);
pst.setString(3, repassword);
pst.setString(4,"Active" );
pst.setDate(5,dateNow);
int i=pst.executeUpdate();
if(i>0)
{
String emailContent = "Dear-'"+mailid+"'Thanks for registration in http://indiaswap.com \n Support Team \n indiaswap.com";
send();
// sendMessage(smtpServ, to, from, subject, emailContent);
out.println("<script>alert('Register Successfully');window.location='newuser/index.jsp';</script>");
}
else
{
out.println("<script>alert('Some error');window.location='newuser/register.jsp';</script>");
}
}
}
catch(Exception e)
{
System.out.println(e);
}
}
public void send()
{
Properties props = new Properties();
props.put("mail.smtp.user", "naveen.rajput11123@gmail.com");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "25");
props.put("mail.debug", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.EnableSSL.enable", "true");
props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.smtp.socketFactory.fallbac k", "false");
props.setProperty("mail.smtp.port", "465");
props.setProperty("mail.smtp.socketFactory.port", "465");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("naveen.rajput11123@gmail.com", password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("naveen.rajput11123@gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("abh0288@gmail.com"));
message.setSubject("Registartion Mail");
message.setText("How are you");
Transport.send(message);
System.out.println("Done");
} catch (Exception e) {
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import Db.databse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author Naveen&Jyoti
*/
@WebServlet(urlPatterns = {"/Register.action"})
public class Register extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try
{
Date dateNow;
dateNow = new java.sql.Date(System.currentTimeMillis());
String ref=request.getParameter("ref");
String mailid=request.getParameter("mailid");
String repassword=request.getParameter("repassword");
databse db=new databse();
Connection con=db.connectit();
String sql="select * from register where mailid=?";
PreparedStatement pste=con.prepareStatement(sql);
pste.setString(1,request.getParameter("mailid"));
ResultSet rs=pste.executeQuery();
int count = 0;
out.println(""+request.getParameter("mailid"));
while(rs.next())
{
count = count + 1;
}
if(count > 1)
{
out.println("<script>alert('Emailid Already Exists!');window.location='newuser/index.jsp';</script>");
}
else if(count==0)
{
PreparedStatement pst=con.prepareStatement("insert into register(registerid,mailid,password,status,date) values(?,?,?,?,?)");
pst.setString(1, ref);
pst.setString(2, mailid);
pst.setString(3, repassword);
pst.setString(4,"Active" );
pst.setDate(5,dateNow);
int i=pst.executeUpdate();
if(i>0)
{
String emailContent = "Dear-'"+mailid+"'Thanks for registration in http://indiaswap.com \n Support Team \n indiaswap.com";
send();
// sendMessage(smtpServ, to, from, subject, emailContent);
out.println("<script>alert('Register Successfully');window.location='newuser/index.jsp';</script>");
}
else
{
out.println("<script>alert('Some error');window.location='newuser/register.jsp';</script>");
}
}
}
catch(Exception e)
{
System.out.println(e);
}
}
public void send()
{
Properties props = new Properties();
props.put("mail.smtp.user", "naveen.rajput11123@gmail.com");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "25");
props.put("mail.debug", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.EnableSSL.enable", "true");
props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.smtp.socketFactory.fallbac k", "false");
props.setProperty("mail.smtp.port", "465");
props.setProperty("mail.smtp.socketFactory.port", "465");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("naveen.rajput11123@gmail.com", password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("naveen.rajput11123@gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("abh0288@gmail.com"));
message.setSubject("Registartion Mail");
message.setText("How are you");
Transport.send(message);
System.out.println("Done");
} catch (Exception e) {
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}