Search code examples
emailsecuritygmailjakarta-mail

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


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


Solution

  • 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;
    }
    

    }