Click here to Skip to main content
15,890,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
While executing below mentioned code,m getting error "javax.mail.AuthenticationFailedException: 535 5.7.3 Authentication unsuccessful.".Although username and password is correct and i used mail.jar and activation.jar.pls help me.thanks in advance


XML
index.html-:
  <!DOCTYPE html>
  <html>
      <head>
          <title>Sending Mail Through JSP</title>
          <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
          <meta name="viewport" content="width=device-width">
      </head>
      <body bgcolor="khaki">
          <form action="maildemo.jsp">
              <table>
                  <tr><td><b><font color="red">To:
                      </td>
                      <td><b><b><input type="text" name="mail" value="Enter receiver mail-id"/><br/>
                      </td>
                  </tr>
                  <tr>
                      <td>
                          <b><font color="red">Subject:
                      </td>
                      <td>
                          <input type="text"  name="sub" value="Enter Subject Line"><br/>
                      </td>
                  </tr>
                  <tr>
                      <td>
                          <b><font color="red">Message Text:
                      </td>
                      <td>
                          <textarea rows="12" cols="80" name="mess"></textarea><br/>
                      </td>
                  </tr>
                  <tr>
                      <td>
                          <input type="submit" value="Send">
                      </td>
                      <td>
                          <input type="reset" value="Reset">
                      </td>
                  </tr>
              </table>
          </form>
      </body>
  </html>


  maildemo.jsp:

  <%@ page import="java.io.*,java.util.*,javax.mail.*,javax.mail.Service"%>
  <%@ page import="javax.mail.internet.*,javax.activation.*"%>
  <%@ page import="javax.servlet.http.*,javax.servlet.*"%>

  <%
      //Creating a result for getting status that messsage is delivered or not!
      String result;
      // Get recipient's email-ID, message & subject-line from index.html page
      final String to = request.getParameter("mail");
      final String subject = request.getParameter("sub");
      final String messg = request.getParameter("mess");

      // Sender's email ID and password needs to be mentioned
      final String from = "username";
      final String pass = "password";

      // Defining the gmail host
      String host = "smtp.gmail.com";



      // Creating Properties object
      Properties props = new Properties();

      // Defining properties
      props.put("mail.smtp.host", host);
      props.put("mail.transport.protocol", "smtp");
      props.put("mail.smtp.starttls.enable", "true");
      props.put("mail.smtp.auth", "true");
      props.put("mail.user", from);
      props.put("mail.password", pass);
      props.put("mail.smtp.port", "465");
      props.put("mail.smtp.socketFactory.port", "465");//465
      props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");




      // Authorized the Session object.
      Session mailSession = Session.getInstance(props, new javax.mail.Authenticator() {
          @Override
          protected PasswordAuthentication getPasswordAuthentication() {
              return new PasswordAuthentication(from, pass);



          }
      });


      try {
          // Create a default MimeMessage object.
          MimeMessage message = new MimeMessage(mailSession);
          // Set From: header field of the header.
          message.setFrom(new InternetAddress(from));




          // Set To: header field of the header.
          message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
          // Set Subject: header field
          message.setSubject(subject);
          // Now set the actual message




          message.setText(messg);
          // Send message

          Transport trans = mailSession.getTransport("smtp");


           trans.send(message);
           System.out.println("Sent message successfully....");

           trans.close();


          result = "Your mail sent successfully....";
      } catch (MessagingException mex)
      {
          mex.printStackTrace();
          result = "Error: unable to send mail....";
      }
  %>
  <title>Sending Mail in JSP</title>
  <h1>
      <center>
          <font color="blue">Sending Mail Using JSP</font>
  </h1>
  <b><center>
          <font color="red"> <% out.println(result);%>

  </b>
Posted
Comments
Kornfeld Eliyahu Peter 7-Jul-15 3:05am    
"Although username and password is correct" - maybe, but the error says otherwise...or you may have no right to do what you try to do...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900