отправлять почту через SMTP Gmail Oauth2 вопрос

Я могу аутентифицироваться на SMTP, следуяпример Google.

Но проблема в том, что я не могу отправить почту. Я получаю следующую ошибку всякий раз, когда пытаюсь отправить письмо.

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 f5sm19578172pav.22

    at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2114)
    at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1618)
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1119)
    at javax.mail.Transport.send0(Transport.java:195)
    at javax.mail.Transport.send(Transport.java:124)

Во-первых, я не могу подключиться к локальному хосту, поэтому я добавил несколько строк кода для правильной сессии, как показано ниже, в класс OAuth2Authenticator, который все еще успешно аутентифицирован. Но не могу отправить письмо.

props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");

Я используютот же объект сеанса и транспортный объект SMTP который будет возвращен из класса OAuth2Authenticator при первой успешной аутентификации.

            Message message = new MimeMessage(session);


        message.setFrom(new InternetAddress(msg.getFrom()));
        message.setRecipients(Message.RecipientType.TO,
            InternetAddress.parse(msg.getTo()));
        message.setSubject(msg.getSubject());
        message.setText(msg.getBody());

        transport.send(message);

Полный журнал отладки

DEBUG: setDebug: JavaMail version 1.4.5
DEBUG SMTP: enable SASL
DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 587, isSSL false
220 mx.google.com ESMTP gj1sm1027169pbc.11 - gsmtp
DEBUG SMTP: connected to host "smtp.gmail.com", port: 587

EHLO E6410-PC
250-mx.google.com at your service, [202.xxx.xxx.xxx]
250-SIZE 35882577
250-8BITMIME
250-STARTTLS
250 ENHANCEDSTATUSCODES
DEBUG SMTP: Found extension "SIZE", arg "35882577"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
STARTTLS
220 2.0.0 Ready to start TLS
EHLO E6410-PC
250-mx.google.com at your service, [202.xxx.xxx.xxx]
250-SIZE 35882577
250-8BITMIME
250-AUTH LOGIN PLAIN XOAUTH XOAUTH2
250 ENHANCEDSTATUSCODES
DEBUG SMTP: Found extension "SIZE", arg "35882577"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XOAUTH XOAUTH2"
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Authenticate with SASL
DEBUG SMTP: SASL mechanisms allowed: XOAUTH2
DEBUG SMTP: SASL AUTH command trace suppressed
DEBUG SMTP SASL: Mechanisms: XOAUTH2
DEBUG SMTP SASL: SASL client XOAUTH2
DEBUG SMTP SASL: callback length: 1
DEBUG SMTP SASL: callback 0: javax.security.auth.callback.NameCallback@100ab23
DEBUG SMTP SASL: no response
Successfully authenticated to SMTP.
NOOP
250 2.0.0 OK gj1sm1027169pbc.11 - gsmtp
Is SMTP still connected: true
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: enable SASL
DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 587, isSSL false
220 mx.google.com ESMTP az8sm1466819pab.3 - gsmtp
DEBUG SMTP: connected to host "smtp.gmail.com", port: 587

EHLO E6410-PC
250-mx.google.com at your service, [202.xxx.xxx.xxx]
250-SIZE 35882577
250-8BITMIME
250-STARTTLS
250 ENHANCEDSTATUSCODES
DEBUG SMTP: Found extension "SIZE", arg "35882577"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
STARTTLS
220 2.0.0 Ready to start TLS
EHLO E6410-PC
250-mx.google.com at your service, [202.xxx.xxx.xxx]
250-SIZE 35882577
250-8BITMIME
250-AUTH LOGIN PLAIN XOAUTH XOAUTH2
250 ENHANCEDSTATUSCODES
DEBUG SMTP: Found extension "SIZE", arg "35882577"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XOAUTH XOAUTH2"
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: use8bit false
MAIL FROM:<[email protected]>
530-5.5.1 Authentication Required. Learn more at
530 5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 az8sm1466819pab.3 - gsmtp
DEBUG SMTP: got response code 530, with response: 530-5.5.1 Authentication Required. Learn more at
530 5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 az8sm1466819pab.3 - gsmtp

RSET
250 2.1.5 Flushed az8sm1466819pab.3 - gsmtp
DEBUG SMTP: MessagingException while sending
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 az8sm1466819pab.3 - gsmtp

    at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2114)
    at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1618)
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1119)
    at javax.mail.Transport.send0(Transport.java:195)

Отредактировано: я теперь вызвал команду AUTH явно так.

 byte[] response = String.format("user=%s\1auth=Bearer %s\1\1",
                    emailAddress, accessToken.getToken()).getBytes();
            response = BASE64EncoderStream.encode(response);
            System.out.println("token check : " + new String(response));

            transport.issueCommand("AUTH XOAUTH2 " + new String(response), 235);

но ответ все тот же.

DEBUG: setDebug: JavaMail version 1.4.5
DEBUG SMTP: enable SASL
DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 587, isSSL false
220 mx.google.com ESMTP kl3sm6965596pbc.15 - gsmtp
DEBUG SMTP: connected to host "smtp.gmail.com", port: 587

EHLO E6410-PC
250-mx.google.com at your service, [xxx.xxx.xxx.xxx]
250-SIZE 35882577
250-8BITMIME
250-STARTTLS
250 ENHANCEDSTATUSCODES
DEBUG SMTP: Found extension "SIZE", arg "35882577"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
STARTTLS
220 2.0.0 Ready to start TLS
EHLO E6410-PC
250-mx.google.com at your service, [xxx.xxx.xxx.xxx]
250-SIZE 35882577
250-8BITMIME
250-AUTH LOGIN PLAIN XOAUTH XOAUTH2
250 ENHANCEDSTATUSCODES
DEBUG SMTP: Found extension "SIZE", arg "35882577"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XOAUTH XOAUTH2"
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
Successfully authenticated to SMTP.
token check : abcdefgFuYUBnbWFpbC5jb20BYXV0aD1CZWabcdefgSEVTNlpUVlFTUXZfblZfZ01FaDF6WWtSQ2kzb1d5RElFQ19mb2NmWG5Sam93RUlBYVJRTldfNwEB
AUTH XOAUTH2 abcdefgFuYUBnbWFpbC5jb20BYXV0aD1CZWabcdefgSEVTNlpUVlFTUXZfblZfZ01FaDF6WWtSQ2kzb1d5RElFQ19mb2NmWG5Sam93RUlBYVJRTldfNwEB
235 2.7.0 Accepted
NOOP
250 2.0.0 OK kl3sm6965596pbc.15 - gsmtp
Is SMTP still connected: true
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: enable SASL
DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 587, isSSL false
220 mx.google.com ESMTP vo6sm6969236pbc.8 - gsmtp
DEBUG SMTP: connected to host "smtp.gmail.com", port: 587

EHLO E6410-PC
250-mx.google.com at your service, [xxx.xxx.xxx.xxx]
250-SIZE 35882577
250-8BITMIME
250-STARTTLS
250 ENHANCEDSTATUSCODES
DEBUG SMTP: Found extension "SIZE", arg "35882577"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
STARTTLS
220 2.0.0 Ready to start TLS
EHLO E6410-PC
250-mx.google.com at your service, [xxx.xxx.xxx.xxx]
250-SIZE 35882577
250-8BITMIME
250-AUTH LOGIN PLAIN XOAUTH XOAUTH2
250 ENHANCEDSTATUSCODES
DEBUG SMTP: Found extension "SIZE", arg "35882577"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XOAUTH XOAUTH2"
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: use8bit false
MAIL FROM:<[email protected]>
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
DEBUG SMTP: got response code 530, with response: 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

RSET
250 2.1.5 Flushed vo6sm6969236pbc.8 - gsmtp
DEBUG SMTP: MessagingException while sending
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

    at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2114)
    at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1618)
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1119)
    at javax.mail.Transport.send0(Transport.java:195)
    at javax.mail.Transport.send(Transport.java:124)

QUIT
221 2.0.0 closing connection vo6sm6969236pbc.8 - gsmtp
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

    at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2114)
    at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1618)
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1119)
    at javax.mail.Transport.send0(Transport.java:195)
    at javax.mail.Transport.send(Transport.java:124)

Ответы на вопрос(6)

Ваш ответ на вопрос