Is there a work around google disabling "Less secure apps"?
Asked Answered
Q

5

35

So since the 31 of may google has disabled the option for "Less secure apps", so I have been using the Java mail API, and since the update i can no longer send emails using the Gmail smtp.

This is the error I'm getting:

javax.mail.AuthenticationFailedException: 535-5.7.8 Username and Password not accepted. Learn more at
535 5.7.8  https://support.google.com/mail/?p=BadCredentials n13-20020a5d400d000000b0020ff7246934sm4970874wrp.95 - gsmtp

I switched to an outlook mail and it seems to work fine, but i was wondering if there is a way to use a Gmail account

Less secure apps & your Google Account

Qua answered 2/6, 2022 at 17:41 Comment(1)
Adapt your code and use oAuth2 authentication: javaee.github.io/javamail/OAuth2Uterine
Q
7

So thanks for all the replays! i have fixed this issue by doing this:

I have enabled the "App password for windows machines" Then i simply changed the password from the email password to the google generated one

and changed the code to the following:

public class JavaMailUtil {
public static void sendMail(String recepient,String order) throws Exception {

    Properties properties=new Properties();
    properties.put("mail.smtp.auth", "true");
    properties.put("mail.smtp.starttls.enable", "true");
    properties.put("mail.smtp.host", "smtp.gmail.com");
    properties.put("mail.smtp.port", "587");
    String myAccountEmail="[email protected]";
    String password="Generated Windows machine password from google";
    Session session=Session.getInstance(properties, new Authenticator() {
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(myAccountEmail, password);
        }
    });
    
    Message message=prepareMessage(session,myAccountEmail,recepient,order);
    Transport.send(message);
    System.out.println("Message Sent successfully");
}

private static Message prepareMessage(Session session,String from,String to,String orderInfo) {
    Message message = new MimeMessage(session);
    try {
        
        message.setFrom(new InternetAddress(from));
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));a
        message.setSubject("Your subject here");
        message.setText(");
        return message;
    } catch (AddressException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

}

Qua answered 8/6, 2022 at 11:50 Comment(0)
L
43

You could try authentification via "App password".

On your Google account:

  1. set 2-Step Verification ON

  2. create 16-character "App password"( How to create app password) -> result should be similar to: 16-character "App password"

  3. Instead of Google account password use 16-character password

    MailMessage mail = new MailMessage();
    foreach (string receiver in DolociPrejemnike())
        mail.To.Add(receiver);
    mail.From = new MailAddress("[email protected]", "No replay"); //pošiljatelj (vedno enak)
    mail.Subject = SetSubject();
    mail.Body = SetBody();
    mail.IsBodyHtml = true;
    
    SmtpClient smtp = new SmtpClient();
    smtp.Host = "smtp.gmail.com";
    smtp.Port = 587;
    smtp.UseDefaultCredentials = true;
    smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "xtqapucsmyvqmvxp"); // Enter seders User name and password  
    smtp.EnableSsl = true;
    smtp.Send(mail);
    
Liturgist answered 4/6, 2022 at 11:6 Comment(4)
Yes, I just did this successfully as well. All you have to do is create the new password and simply replace your credentials with it. Everything else can stay the same. Don't have to use xoauth2.Devilment
This should be the accepted answer, also, google recommends "App passwords" as an alternative to less secure apps.Burden
If the code sample were Java, rather than .Net, then it would be perfect.Mccallum
This is the solution.Flashy
Q
7

So thanks for all the replays! i have fixed this issue by doing this:

I have enabled the "App password for windows machines" Then i simply changed the password from the email password to the google generated one

and changed the code to the following:

public class JavaMailUtil {
public static void sendMail(String recepient,String order) throws Exception {

    Properties properties=new Properties();
    properties.put("mail.smtp.auth", "true");
    properties.put("mail.smtp.starttls.enable", "true");
    properties.put("mail.smtp.host", "smtp.gmail.com");
    properties.put("mail.smtp.port", "587");
    String myAccountEmail="[email protected]";
    String password="Generated Windows machine password from google";
    Session session=Session.getInstance(properties, new Authenticator() {
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(myAccountEmail, password);
        }
    });
    
    Message message=prepareMessage(session,myAccountEmail,recepient,order);
    Transport.send(message);
    System.out.println("Message Sent successfully");
}

private static Message prepareMessage(Session session,String from,String to,String orderInfo) {
    Message message = new MimeMessage(session);
    try {
        
        message.setFrom(new InternetAddress(from));
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));a
        message.setSubject("Your subject here");
        message.setText(");
        return message;
    } catch (AddressException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

}

Qua answered 8/6, 2022 at 11:50 Comment(0)
S
6

Now that you can no longer use login and password with Googles smtp server the only option really is to use XOauth2

I havent used Jakarta before but it appears to support it. You should look into OAuth2 Support

Properties props = new Properties();
props.put("mail.imap.ssl.enable", "true"); // required for Gmail
props.put("mail.imap.auth.mechanisms", "XOAUTH2");
Session session = Session.getInstance(props);
Store store = session.getStore("imap");
store.connect("imap.gmail.com", username, oauth2_access_token);

Apps password

option two is to go to your google account and generate an apps password

When running your code use the password generated instead of the actual users password. The main issue with this being there is no telling how long google will continue to support apps password.

Shae answered 2/6, 2022 at 20:53 Comment(4)
How do i send emails after google disabling "Less secure apps"?. Coz imap cant send email.Turfman
Not necessary to use this method. Using "App password" as described in the other answer is sufficient.Devilment
Are you sure that google will continue to support apps password? The Xoauth2 option is a much more secure option.Shae
@DalmTo google recommends "App passwords", there's no need to worry about support, they can just use another way if that will happen. I'm sure it will take years before that.Burden
O
2

Note: please enable 2-factor authentication in google account before proceeding.

Less secure apps (https://myaccount.google.com/u/0/lesssecureapps) options is no longer available.

Instead use another way of using apppasswords provided by google.
https://myaccount.google.com/u/0/apppasswords

Use 16 digit code provided by google instead of password and that will serve as authentication token.

enter image description here

Overweary answered 16/1, 2023 at 3:18 Comment(1)
It appears they're trying to aggressively move people off of App Passwords, as the option is no longer listed under Account / Security. The link I found to access the App Password management page was slightly different than what you indicate, it was simply myaccount.google.com/apppasswordsFurrier
F
0

To those who followed the other answers but are still getting the "Authentication Failed" error when using an app password, a key point is that this solution is NOT working for XOAUTH2 if you are using that or are following a guide saying to use oauth2.

So in the following code:

props.put("mail.imap.auth.mechanisms", "XOAUTH2");

Simply change it to the following:

props.put("mail.imap.auth.mechanisms", "XOAUTH");

and it should work, keeping all else the same.

Fact answered 3/8, 2022 at 2:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.