0

The below code worked before but now showing error like javax.mail.AuthenticationFalied

Please help i add library mail.jar and activation.jar

Am getting the email from registered user from mysql database.

<%@page import="javax.mail.internet.InternetAddress"%>
<%@page import="java.util.*" %>
<%@page import="javax.mail.internet.MimeMessage"%>
<%@page import="javax.mail.Session" %>
<%@page import="java.util.Properties"%>
<%@page import="javax.mail.Message"%>
<%@page import="javax.mail.MessagingException"%>
<%@page import="javax.mail.Transport"%>
<%@page import="javax.mail.PasswordAuthentication"%>

     <%@ page language="java" import ="java.sql.*" %>
     <%@ page import="java.io.*" %>
     <%@ page import="java.text.ParseException" %>
     <%@ page import="java.text.SimpleDateFormat" %>
     <%@ page import="java.util.Date" %>

        <%

        String email=request.getParameter("email");

        Connection con=null;
        Statement stmt=null;
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        con=DriverManager.getConnection("jdbc:mysql://localhost:3306/discussion","root","root");
        Statement st1=con.createStatement();
        ResultSet rs1=st1.executeQuery("select uname from userreg where email='"+email+"'");

        if(rs1.next())
        {

        final String username="headind14@gmail.com";
        final String password="password";

        Random ran=new Random();
        String str=new String();
        for(int i=1;i<=5;i++)
            {
            str+=ran.nextInt(10);
            }


        Properties props=new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host","smtp.gmail.com");
        props.put("mail.smtp.port", 587);

        Session session1=Session.getInstance(props,
        new javax.mail.Authenticator() {
        public PasswordAuthentication getPasswordAuthentication(){
        return new PasswordAuthentication(username,password);
        }});    
        try
            {
             stmt=con.createStatement();
             ResultSet rs=stmt.executeQuery("select * from userreg");
             rs.next();
             Statement st=con.createStatement();
             st.executeUpdate("insert into forgot(uname,code) values('"+rs1.getString(1)+"','"+str+"')");


            Message message=new MimeMessage(session1);
            message.setFrom(new InternetAddress("headind14@gmail.com"));

            message.setRecipients( Message.RecipientType.TO,InternetAddress.parse(""+email+""));
            message.setSubject("chakrika infosolution");
            message.setSubject("Forgot Password");
            message.setContent("Hello "+rs1.getString(1)+",<br>Your Secret Code is: "+str+"<br/><a href='http://localhost:8084/Board/board/forgotpasswordd.jsp?uname="+rs1.getString(1)+"&code="+str+"'>Click Here</a> to reset your Password<br/><br/>","text/html; charset=UTF-8");
            Transport.send(message);
            Transport.send(message);

           %>
           <script>
              alert("Email send Successfully");
              window.location="../board/forgotpassword.jsp";
           </script>

           <%

            }
        catch(MessagingException e)
                               {
            out.println(e);
        }

        }
        else
        {        %>
           <script>
              alert("Invalid Email");
              window.location="../board/forgotpassword.jsp";
           </script>

        <%}
        %>
    </body>
</html>
  • 2
    what changed, if it worked before and now not anymore? if you did not change any code and nothing else on your system, then maybe your account got canceled due to you sending too many emails or the like. – hoijui Dec 25 '14 at 13:36
  • Check your firewall. If possible first test using mozilla thunderbird...if that too fails then it's your firewall that is blocking the connection. See this http://stackoverflow.com/questions/27289931/exception-in-sending-email-from-java-via-gmail if you can get some info. – Leo Dec 26 '14 at 09:30

1 Answers1

0

As I saw your code which is correct. Use JavaMail Api 1.4.5 jar or latest version in stead of the jar you have used.

Santosh Karna
  • 119
  • 1
  • 3
  • 12