4

I am attempting to send mail using Java to a Gmail account, code below. I appear to be doing everything correctly, however I receive an authentication failure. Google wants me to turn on the "less secure app" feature to enable transmission.

Is there a way to code in such a way that Gmail is happy with Java and will not throw the "turn on less secure apps" fault?

Error:

javax.mail.AuthenticationFailedException: 534-5.7.14 <https://accounts.google.com/signin/continue?sarp=...U
534-5.7.14 FigguJaZwDtp...
534-5.7.14 ...o> Please log in via your web browser and
534-5.7.14 then try again.
534-5.7.14  Learn more at
534 5.7.14  https://support.google.com/mail/answer/... - gsmtp

Code:

String hostSmtpUser = "myemail@gmail.com";
String host = "smtp.gmail.com";
String hostPort = "587";
String hostSmtpPassword = "thepassword";

Properties properties = System.getProperties();
properties.setProperty("mail.smtp.user", hostSmtpUser);
properties.setProperty("mail.smtp.host", host);
properties.setProperty("mail.smtp.starttls.enable", "true");
properties.setProperty("mail.smtp.port", hostPort);
properties.setProperty("mail.smtp.auth", "true");

Session oSession;
if (true == ToolsCommon.isEmpty(hostSmtpUser))
    oSession = Session.getInstance(properties);
else
    oSession = Session.getInstance(properties, new javax.mail.Authenticator()
    {
        protected PasswordAuthentication getPasswordAuthentication()
        {
        return new PasswordAuthentication(hostSmtpUser, hostSmtpPassword);
        }
    });

// Compose the message  
try
{
    MimeMessage message = new MimeMessage(oSession);
    message.setFrom(new InternetAddress(from));
    message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
    message.setSubject(Subject);
    message.setText(Body);

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

catch (MessagingException ex)
{
    // Log the error.
    ToolsLog.logError(TypeLog.ui, ex);
}

I already did research, so the code to my knowledge is not the problem, just do not see a workaround for the less secure apps message.

References:

Ref 1 Ref 2 Ref 3 Ref 4

Community
  • 1
  • 1
Sarah Weinberger
  • 15,041
  • 25
  • 83
  • 130

2 Answers2

2

By default, GMail doesn't allow password-based authentication -- that's why you'd have to allow "less-secure apps" to use your program as-is.

Instead, you can use OAuth 2.0 to avoid using a password directly. That method is considered secure by Google and won't require changing any account settings.

Steve Trout
  • 9,261
  • 2
  • 19
  • 30
0

Less secure apps (https://myaccount.google.com/u/0/lesssecureapps) options is disabled and we can longer use it to send mail.

But there is a better option provided by google - apppasswords https://myaccount.google.com/u/0/apppasswords

Use 16 digit code provided by google instead of password and that should be it.

enter image description here

Note: 2-factor authentication needed to be enabled before using appspasswords.

Vaibhav Jain
  • 1,939
  • 26
  • 36