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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.