send mail via SMTP Gmail Oauth2 issue

后端 未结 6 594
没有蜡笔的小新
没有蜡笔的小新 2021-01-03 12:06

I can authenticated to SMTP by following the google example.

But the problem now is I can\'t send out the mail. I am getting the following error whenever I try to se

6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-03 12:29

    This is an old thread but it took me a very long time to figure this out after going through numerous forums (and none of them had the right answer) so I'll post it if it helps someone. Many of the answers above suggest using password which is incorrect

    If you are using the latest google apis with OAuth2 and also using the sample code @ http://code.google.com/p/google-mail-oauth2-tools/downloads/detail?name=oauth2-java-sample-20120904.zip&can=2&q=

    and are getting the exception com.sun.mail.smtp.SMTPSendFailedException: 530-5.5.1 Authentication Required. Learn more at 530 5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 vo6sm6969236pbc.8 - gsmtp

    Here are the steps to fix the problem

    Run python oauth2.py exactly as the instruction suggest in the zip download. The OAuth2 Argument is the oauthtoken you must use in the code.

    After running the OAuth2Authenticator.connectToSmtp("smtp.googlemail.com", 587, email, oauthToken, true) method to get the SMTPTransport. To send the message:

      //create the message
      //Note that you do need to get the same session object that gets created within the connectToSmtp method.
      MimeMessage message = new MimeMessage(session);
      message.setFrom(new InternetAddress("from@gmail.com"));
      message.addRecipient(Message.RecipientType.TO, new InternetAddress("to@gmail.com"));
      message.setSubject("This is the Subject Line!");
      message.setText("This is actual message");
    
      //this was the magic line that is needed - for authentication
      transport.issueCommand("AUTH XOAUTH2 " + oauthToken, 235);
    
      //send the message
      transport.sendMessage(message, message.getAllRecipients());
    

提交回复
热议问题