title: Unable to send email to gmail on javamail using xoauth
body: <p>i have been working on a project which involves connecting gmail smtp and sending emails using oauth2 based connection.</p>
  
  <p>im using samples provided <a href="http://code.google.com/p/google-mail-oauth2-tools/source/browse/trunk#trunk/java/com/google/code/samples/oauth2" rel="nofollow">here</a> to connct to gmail smtp and connecting to smtp works fine. however the problem is while sending the message. im getting below error</p>
  
  <blockquote>
    <p>com.google.code.com.sun.mail.smtp.SMTPSendFailedException: 530-5.5.1 Authentication Required. Learn more at 530 5.5.1
    <a href="http://support.google.com/mail/bin/answer.py?answer=14257" rel="nofollow">http://support.google.com/mail/bin/answer.py?answer=14257</a>
    o5sm11801729pay.5</p>
  </blockquote>
  
  <p>im clue less as to why the error pops up after successful authentication with gmail smtp. below is the complete debug log</p>
  
  <pre><code>DEBUG: setDebug: JavaMail version 1.4.4
  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 th10sm14683815pbc.76
  DEBUG SMTP: connected to host "smtp.gmail.com", port: 587
  
  EHLO bala-Inspiron-N4010
  250-mx.google.com at your service, [27.97.28.232]
  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 bala-Inspiron-N4010
  250-mx.google.com at your service, [27.97.28.232]
  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 ""
  AUTH XOAUTH2 dXNlcj1jaGFuZHUyNzA4QGdtYWlsLmNvbQFhdXRoPUJlYXJlciB5YTI5LkFIRVM2WlFqUWJqZ3RKRmpSSlFxM2lrMnd1QXMySEQ1UXVjWm8ySFN1bnMwWlk2VAEB
  235 2.7.0 Accepted
  HELO
  250 mx.google.com at your service
  NOOP
  250 2.0.0 OK th10sm14683815pbc.76
  DEBUG: getProvider() returning com.google.code.javax.mail.Provider[TRANSPORT,smtp,com.google.code.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 qf7sm14690532pbb.49
  DEBUG SMTP: connected to host "smtp.gmail.com", port: 587
  
  EHLO bala-Inspiron-N4010
  250-mx.google.com at your service, [27.97.28.232]
  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 bala-Inspiron-N4010
  250-mx.google.com at your service, [27.97.28.232]
  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:&lt;chandu2708@gmail.com&gt;
  530-5.5.1 Authentication Required. Learn more at
  530 5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 qf7sm14690532pbb.49
  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 qf7sm14690532pbb.49
  
  RSET
  250 2.1.5 Flushed qf7sm14690532pbb.49
  com.google.code.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 qf7sm14690532pbb.49
  </code></pre>
  
  <p>below is the code im using to send email</p>
  
  <pre><code>Properties props = new Properties();
          props.put("mail.smtp.starttls.enable", "true");
          props.put("mail.smtp.starttls.required", "true");
          props.put("mail.smtp.sasl.enable", "true");
          props.put("mail.smtp.host", "smtp.gmail.com");
          props.put("mail.smtp.port", "587");
          props.put("mail.imaps.sasl.mechanisms", "XOAUTH2");
  
          props.put(OAuth2SaslClientFactory.OAUTH_TOKEN_PROP, oauthToken);
          //props.put("mail.smtp.password", "");
  
          // props.put("mail.smtp.auth", "true");
  
          Session session = Session.getInstance(props);
          session.setDebug(debug);
  
          final URLName unusedUrlName = null;
          SMTPTransport transport = new SMTPTransport(session, unusedUrlName);
          // If the password is non-null, SMTP tries to do AUTH LOGIN.
          final String emptyPassword = null;
          transport.connect(host, port, userEmail, emptyPassword);
  
          byte[] response = String.format("user=%s\1auth=Bearer %s\1\1",
                  userEmail, oauthToken).getBytes();
          response = BASE64EncoderStream.encode(response);
  
          transport.issueCommand("AUTH XOAUTH2 " + new String(response), 235);
  
          return new SMTPConnection(transport, session, new String(response));
  
             --------------XXX end of method XXX-----------------
  
  
  SMTPConnection smtpConnection = XoauthAuthenticator.connectToSmtp("smtp.gmail.com", 587,
                  user.getEmail(), user.getAccessToken(), null, XoauthAuthenticator.getAnonymousConsumer(),
                  true);
  
          SMTPMessage smtpMessage = new SMTPMessage(smtpConnection.getSession());
          smtpMessage.setFrom(new InternetAddress(user.getEmail()));
          smtpMessage.setRecipients(RecipientType.TO, InternetAddress.parse(email));
  
          String subject = message;
  
          smtpMessage.setSubject(subject);
  
          smtpMessage.setText(message);
  
          smtpMessage.setSentDate(new Date());
  
  
          smtpConnection.getSMTPTransport().simpleCommand("HELO");
  
          boolean isConnected = smtpConnection.getSMTPTransport().isConnected();
  
          smtpConnection.getSMTPTransport().send(smtpMessage, smtpMessage.getAllRecipients());
  </code></pre>
  
  <p>Your help is greatly appreciated.</p>
  
  <p>Thank you,
  -bala. </p>