1

The code I'm using to send a simple mail:

import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import java.io.*;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;

public class SendMailUsingAuthentication
    {
    public static final String SMTP_HOST_NAME = "smtp.gmail.com"; 
    public static final String SMTP_AUTH_USER = "username@gmail.com";
    public static final String SMTP_AUTH_PWD = "password";

    public static final String emailMsgTxt ="error found wgile clicking on an element";
    public static final String emailSubjectTxt ="Test Email";
    public static final String emailFromAddress ="hiphop.sujai@gmail.com";

    public static final String[] emailList = {"receiver.username@gmail.com"};

public void postMail(String recipients [], String subject, String message, String from) throws MessagingException

    {
        boolean debug = false;

        Properties props = new Properties();

        props.put("mail.smtp.starttls.enable","true");
        props.put("mail.smtp.host", SMTP_HOST_NAME);
        props.put("mail.smtp.auth", "true");

        Authenticator auth = new SMTPAuthenticator();
        Session session = Session.getDefaultInstance(props, auth);
        session.setDebug(debug);

        Message msg = new MimeMessage(session);

        InternetAddress addressFrom = new InternetAddress(from);
        msg.setFrom(addressFrom);

        InternetAddress[] addressTo = new InternetAddress[recipients.length];
        for (int i = 0; i < recipients.length; i++) {
            addressTo[i] = new InternetAddress(recipients[i]);
        }
        msg.setRecipients(Message.RecipientType.TO, addressTo);

        msg.setSubject(subject);

        msg.setContent(message, "text/plain");

        Transport.send(msg);
        System.out.println("Successfully sent mail to all users");

    }


private class SMTPAuthenticator extends javax.mail.Authenticator
{
    public PasswordAuthentication getPasswordAuthentication()
    {
        String username = SMTP_AUTH_USER;
        String password = SMTP_AUTH_PWD;
        return new PasswordAuthentication(username, password);
    }
}

}

When I run the above code I got following exception, user name that I provide and the password that I provided are correct. I also tried giving just the username without adding @gmail.com still it doesn't work.

Exception in thread "main" javax.mail.AuthenticationFailedException: 534-5.7.14
<https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbvTi
534-5.7.14 QLG6LXdGFhnbFAdBRTtZO4ynMurcJa7KsqyOjGLcdS9lAyFP42I6l75P9B_90NPxQr-Bj-
534-5.7.14 0Yzk9vWoigAtOgTD3_D4vp4fEJ7C8oCtFTj4kRJfRJwJJvgPzdZ7ujc1zNyksYMYhLBaIP
534-5.7.14 LpYpcxJKLz90-rBx5NdWeJTu_1U1FENAX4QxVjsYaeeuJxMjfk6c1aZBMm2e485o3PV2H4
534-5.7.14 9rQ-6HA> Please log in via your web browser and then try again.
534-5.7.14 Learn more at
534 5.7.14 https://support.google.com/mail/bin/answer.py?answer=78754
om7sm11753908pdb.61 - gsmtp

 at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate
(SMTPTransport.java:648)
 at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:583)
 at javax.mail.Service.connect(Service.java:313)
 at javax.mail.Service.connect(Service.java:172)
 at javax.mail.Service.connect(Service.java:121)
 at javax.mail.Transport.send0(Transport.java:190)
 at javax.mail.Transport.send(Transport.java:120)
 at SendMailUsingAuthentication.postMail(SendMailUsingAuthentication.java:87)
 at Capture_Screen.main(Capture_Screen.java:32)
halfer
  • 19,824
  • 17
  • 99
  • 186

3 Answers3

5

I just had the same issue. It turns out Google blocks suspicious attempts to access mail accounts. I just logged in to gmail in my browser and there was an error/warning message at the top of the window alerting me of suspicious activity. After clicking on the message I was able to allow the connection from the "suspicious" host which was indeed my app running on AWS.

Fabian
  • 3,310
  • 3
  • 26
  • 35
3

click on the link https://www.google.com/settings/security/lesssecureapps?zx=xvonmtjnhxau and enable less secure app. Then access from java...it works...

Mohammed mansoor
  • 743
  • 3
  • 11
  • 18
-1

The reason behind this is that Gmail detects a less secure authentication method, so they give authentication error. They do this for your security.

You can use a more secure authentication, like OAuth 2.0.

There is also a way to allow less secure authentication. It can be enabled under security settings for your Gmail account.

totoro
  • 2,469
  • 2
  • 19
  • 23
Susampath
  • 706
  • 10
  • 13
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/18066776) – kometen Nov 26 '17 at 16:35
  • Thanks a lot for your guidance - kometen – Susampath Nov 28 '17 at 06:54