0

I'm trying to send email using java API and i'm giving the right emailid and password but still i get AuthenticationFailedException.

I also tried giving host=mail.smtp.port and changing port to 587 still i end up getting the same error..

Please help me where i'm going wrong..?

public class SendEmail
   {
   public static void main(String [] args)
   {    
  // Recipient's email ID needs to be mentioned.
  String to = "[email protected]";

  // Sender's email ID needs to be mentioned
  final String from = "[email protected]";

  // Assuming you are sending email from localhost
  final String host = "smtp.googlemail.com";

  // Get system properties
  Properties properties = System.getProperties();

  // Setup mail server
  properties.setProperty("mail.smtp.host", host);
  properties.setProperty("mail.smtp.starttls.enable", "true");
  properties.setProperty("mail.smtp.user", from);
  //properties.setProperty("mail.smtp.password", "xyz");

  properties.setProperty("mail.debug", "false");
  properties.setProperty("mail.smtp.auth", "true"); 
//  properties.setProperty("mail.smtp.port", "587");

  // Get the default Session object.
 // Session session = Session.getDefaultInstance(properties);

  Session session = Session.getInstance(properties,
          new javax.mail.Authenticator() {

              protected PasswordAuthentication getPasswordAuthentication() {
                  return new PasswordAuthentication(from, "xyz");
              }
          });

  session.setDebug(true);

  try{
     // Create a default MimeMessage object.
     MimeMessage message = new MimeMessage(session);

     // 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("This is the Subject Line!");

     // Now set the actual message
     message.setText("This is actual message");

     // Send message
    // Transport.send(message);


     Transport.send(message);
     System.out.println("Sent message successfully....");
  }catch (MessagingException mex) {
     mex.printStackTrace();
  }
 }
}

Error:

javax.mail.AuthenticationFailedException
at javax.mail.Service.connect(Service.java:306)
at javax.mail.Service.connect(Service.java:156)
at javax.mail.Service.connect(Service.java:105)
at javax.mail.Transport.send0(Transport.java:168)
at javax.mail.Transport.send(Transport.java:98)
0

1 Answer 1

3

Just check if you have enabled logging in from "Less Secure Apps" using this link. This setting needs to be enabled for the account [email protected].

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.