Sending Email through servlets and JSP

Reply

Join Date: May 2008
Posts: 5
Reputation: Dolly-Sweety is an unknown quantity at this point 
Solved Threads: 0
Dolly-Sweety Dolly-Sweety is offline Offline
Newbie Poster

Sending Email through servlets and JSP

 
0
  #1
Jun 4th, 2008
Hello friends,

I faced a problem in sending email through servlet. I have downloaded mail.jar and activation.jar. All is running perfect but error is coming.

javax.mail.MessagingException: [EOF]

import java.io.*;
import java.net.*;
import java.util.*;
import java.sql.*;
import javax.mail.*;
import javax.mail.internet.*;

import javax.servlet.*;
import javax.servlet.http.*;

/**
 *
 * @author Administrator
 */
public class SendPasswordCandidate extends HttpServlet {
   
    /** 
    * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
    * @param request servlet request
    * @param response servlet response
    */
    
    Connection con;
    Statement stat;
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet SendPasswordCandidate</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet SendPasswordCandidate at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
            */
        } finally { 
            out.close();
        }
    } 

    // <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
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        //processRequest(request, response);
         response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String email=request.getParameter("email");
        String name=null;
        String password=null;
        String next;
        if(email!=null)
        {
        try
        {
                Class.forName("com.mysql.jdbc.Driver");
                con = DriverManager.getConnection("jdbc:mysql://localhost:3306/Recruitment","root","krishna");
                stat=con.createStatement();
                String sql="Select Candidate_First_Name from candidate_detail where Candidate_Email='"+email+"'";
                ResultSet rs1=stat.executeQuery(sql);
                while(rs1.next())
                    name=rs1.getString("Candidate_First_Name");
                
                String query="Select Candidate_Password from candidate_detail where Candidate_Email='"+email+"'";
                ResultSet rs2=stat.executeQuery(query);
                while(rs2.next())
                    password=rs2.getString("Candidate_Password");
                    
                String mailServer="smtp.mail.yahoo.com";
                //String mailServer="mail.netonecom.net";
                String subject="Confirm Password";
                String[] to={email};
                String from="NoWonder<info@nowonder.co.in>";
                String messageText="Welcome "+name+"\n"+"Your Password is "+password;
                sendMail(mailServer,subject,to,from,messageText);
                
                next="/PaswordSend.jsp";
                
        }
        catch(SQLException se)
        {
            out.println(se);
        }
        catch(ClassNotFoundException cnf)
        {
            out.println(cnf);
        }
        catch(AddressException ae)
        {
            out.println(ae);
        }
        catch(MessagingException me)
        {
            out.println(me);
        }
        }
        else
        {
            next="/ForgotPassword.jsp";
            out.println("Please enter the Email address");
        }
        
        
    }
    public void sendMail(String mailServer,String subject,String to[],String from,String messageText) throws AddressException,MessagingException
    {
        Properties mailprops=new Properties();
        mailprops.put("mail.smtp.host", mailServer);
        
        Session mailsession=Session.getDefaultInstance(mailprops,null);
        int toCount=to.length;
        InternetAddress[] toAddrs=new InternetAddress[toCount];
        for(int i=0;i<toCount;i++)
        {
            toAddrs[i]=new InternetAddress(to[i]);
        }
        InternetAddress fromAddr=new InternetAddress(from);
        Message message=new MimeMessage(mailsession);
        message.setFrom(fromAddr);
        message.setRecipients(Message.RecipientType.TO, toAddrs);
        message.setSubject(subject);
        message.setContent(messageText.toString(),"text/plain");
        
        Transport.send(message);
        
        
     }

Then I thought that I am using BSNL dialup connection for the internet so I tried with this code.

import java.io.*;
import java.net.*;
import java.util.*;
import java.sql.*;
import javax.mail.*;
import javax.mail.internet.*;

import javax.servlet.*;
import javax.servlet.http.*;

/**
 *
 * @author Administrator
 */
public class SendPasswordCandidate extends HttpServlet {
   
    /** 
    * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
    * @param request servlet request
    * @param response servlet response
    */
    
    Connection con;
    Statement stat;
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet SendPasswordCandidate</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet SendPasswordCandidate at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
            */
        } finally { 
            out.close();
        }
    } 

    // <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
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        //processRequest(request, response);
         response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String email=request.getParameter("email");
        String name=null;
        String password=null;
        String next;
        if(email!=null)
        {
        try
        {
                Class.forName("com.mysql.jdbc.Driver");
                con = DriverManager.getConnection("jdbc:mysql://localhost:3306/Recruitment","root","krishna");
                stat=con.createStatement();
                String sql="Select Candidate_First_Name from candidate_detail where Candidate_Email='"+email+"'";
                ResultSet rs1=stat.executeQuery(sql);
                while(rs1.next())
                    name=rs1.getString("Candidate_First_Name");
                
                String query="Select Candidate_Password from candidate_detail where Candidate_Email='"+email+"'";
                ResultSet rs2=stat.executeQuery(query);
                while(rs2.next())
                    password=rs2.getString("Candidate_Password");
                    
               // String mailServer="smtp.mail.yahoo.com";
                String mailServer="mail.netonecom.net";
                String subject="Confirm Password";
                String[] to={email};
                String from="NoWonder<info@nowonder.co.in>";
                String messageText="Welcome "+name+"\n"+"Your Password is "+password;
                sendMail(mailServer,subject,to,from,messageText);
                
                next="/PaswordSend.jsp";
                
        }
        catch(SQLException se)
        {
            out.println(se);
        }
        catch(ClassNotFoundException cnf)
        {
            out.println(cnf);
        }
        catch(AddressException ae)
        {
            out.println(ae);
        }
        catch(MessagingException me)
        {
            out.println(me);
        }
        }
        else
        {
            next="/ForgotPassword.jsp";
            out.println("Please enter the Email address");
        }
        
        
    }
    public void sendMail(String mailServer,String subject,String to[],String from,String messageText) throws AddressException,MessagingException
    {
        Properties mailprops=new Properties();
        mailprops.put("mail.smtp.host", mailServer);
        
        Session mailsession=Session.getDefaultInstance(mailprops,null);
        int toCount=to.length;
        InternetAddress[] toAddrs=new InternetAddress[toCount];
        for(int i=0;i<toCount;i++)
        {
            toAddrs[i]=new InternetAddress(to[i]);
        }
        InternetAddress fromAddr=new InternetAddress(from);
        Message message=new MimeMessage(mailsession);
        message.setFrom(fromAddr);
        message.setRecipients(Message.RecipientType.TO, toAddrs);
        message.setSubject(subject);
        message.setContent(messageText.toString(),"text/plain");
        
        Transport.send(message);
        
        
     }

Then also error comes i.e.

javax.mail.SendFailedException: Invalid Addresses; nested exception is: com.sun.mail.smtp.SMTPAddressFailedException: 553 sorry, that domain isn't in my list of allowed rcpthosts (#5.7.1)

Sometimes javax.mail.MessagingException:Unable to connect SMTP port:25,NestedException:Unable to rely on "abcd@yahoo.co.in.

I am really confused. I thought that as I am using BSNL dialup connection so my ISP(Internet Service Provider) is BSNL netone so what should I use in variable mailServer in
mailprops.put("mail.smtp.host", mailServer);


Please help me as early as possible as I am having time constraint.

Thank You.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Sending Email through servlets and JSP

 
0
  #2
Jun 5th, 2008
quite simple. Yahoo is smart and doesn't allow just anyone to send mail using their servers.
You'll find almost every mailserver on the internet to be that smart, because the stupid ones have long been turned into spam havens and blocked from sending mail TO anyone because of that.

And as you're behaving like a spammer by trying to use Yahoo's mailserver without permission, you get an error.
Count yourself lucky that you're not blocked from using them at all if you persist.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the JSP Forum
Thread Tools Search this Thread



Tag cloud for JSP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC