title: Is it possible to send e-mails with GMail API using user name and password only?
body: <p>I was told by a colleague that using the <a href="https://developers.google.com/api-client-library/java/apis/gmail/v1" rel="noreferrer">Gmail API</a> library, you can send e-mails from corporate Gmail account with user name and password.</p>
  
  <p>So far I could not find out how exactly I can build an instance of the <a href="https://developers.google.com/resources/api-libraries/documentation/gmail/v1/java/latest/com/google/api/services/gmail/Gmail.html" rel="noreferrer">Gmail</a> with user name and password (no access token, no secret).</p>
  
  <p>Is it at all possible to use GMail API for sending e-mails with just user name and password? If so, where can I find an example?</p>
  
  <p><strong>Update 1:</strong> When I use the following code</p>
  
  <pre><code>@Test
  fun sendMail() {
      val credentials = Properties()
      credentials.load(FileInputStream("src/test/resources/credentials.properties"))
  
      val username = credentials.getProperty("username")
      val password = credentials.getProperty("password")
  
      val prop = 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
  
      val session = Session.getInstance(prop,
              object : javax.mail.Authenticator() {
                  override fun getPasswordAuthentication(): PasswordAuthentication {
                      return PasswordAuthentication(username, password)
                  }
              })
  
      try {
  
          val message = MimeMessage(session)
          message.setFrom(InternetAddress("..."))
          message.setRecipients(
                  Message.RecipientType.TO,
                  InternetAddress.parse("...")
          )
          message.setSubject("Testing Gmail")
          message.setText("Hello!!")
  
          Transport.send(message)
  
          println("Done")
  
      } catch (e: MessagingException) {
          e.printStackTrace()
      }
  }
  </code></pre>
  
  <p>I get this exception:</p>
  
  <blockquote>
    <p>javax.mail.AuthenticationFailedException: 535-5.7.8 Username and Password not accepted. Learn more at
    535 5.7.8  <a href="https://support.google.com/mail/?p=BadCredentials" rel="noreferrer">https://support.google.com/mail/?p=BadCredentials</a> n6sm4681584wmn.48 - gsmtp</p>
  </blockquote>