javax.mail.AuthenticationFailedException: 535-5.7.8 Username and Password not accepted

前端 未结 10 1442
臣服心动
臣服心动 2020-12-03 07:33

I am getting this error when I try to send mail using the JavaMail API:

javax.mail.AuthenticationFailedException: 535-5.7.8 Username and Password not accept         


        
10条回答
  •  情歌与酒
    2020-12-03 08:03

    1.Allow Less Secure App(should be turned on).

    2.Check Gmail Username and Password..

     public static void main(String[] args) {
    
            final String username = "YourMailId";
            final String password = "password";
    
            Properties prop = new Properties();
            prop.put("mail.smtp.host", "smtp.gmail.com");
            prop.put("mail.smtp.port", "587");
            prop.put("mail.smtp.auth", "true");
            prop.put("mail.smtp.starttls.enable", "true"); //TLS
    
            Session session = Session.getInstance(prop,
                    new javax.mail.Authenticator() {
                        protected PasswordAuthentication getPasswordAuthentication() {
                            return new PasswordAuthentication(username, password);
                        }
                    });
    
            try {
    
                Message message = new MimeMessage(session);
                message.setFrom(new InternetAddress("Tarunsunny143@gmail.com"));
                message.setRecipients(
                        Message.RecipientType.TO,
                        InternetAddress.parse("balachandralucky2@gmail.com, to_username_b@yahoo.com")
                );
                message.setSubject("Testing Gmail TLS");
                message.setText("Dear Mail Crawler,"
                        + "\n\n Please do not spam my email!");
    
                Transport.send(message);
    
                System.out.println("Done");
    
            } catch (MessagingException e) {
                e.printStackTrace();
            }
        }
    
    }
    

提交回复
热议问题