title: nodemailer gmail connection closed with high number of mails
body: <p>I have exactly the same issue as this post :
  <a href="https://stackoverflow.com/questions/41621932/send-multipe-emails-using-nodemailer-and-gmail">Send multipe emails using nodemailer and gmail</a></p>
  
  <p>When sending too many emails with nodemailer and gmail, I get a 421 error, referring to too many concurrent sessions.</p>
  
  <p>What can i do to avoid opening too many sessions ?</p>
  
  <p>I already contact google who confirm me I wasn't blocked by any limitation ( I didn't reach the number of daily mail and there is no limit on mail/minute).
  I tried to wait for each mail to be sent before sending a new one; to create and close new transport at each mail, but I keep getting this error after approximately the 100th email. </p>
  
  <p>Here the complete error:</p>
  
  <pre><code>{ [Error: Mail command failed: 421 4.7.0 Try again later, closing 
  connection. (MAIL) e17sm2124566ede.14 - gsmtp]
  code: 'EENVELOPE',
  response: '421 4.7.0 Try again later, closing connection. (MAIL) 
  e17sm2124566ede.14 - gsmtp',
  responseCode: 421,
  command: 'MAIL FROM' }
  </code></pre>
  
  <p>And my code :</p>
  
  <p>Nodemailer settings :</p>
  
  <pre><code>function setMailTransport () {
    return nodemailer.createTransport(smtpTransport({
      service: 'gmail',
      ignoreTLS: true,
      auth: {
        xoauth2: xoauth2.createXOAuth2Generator({
          user: 'xxxxxx',
          clientId: 'xxxxxx',
          clientSecret: 'xxxxxx',
          refreshToken: 'xxxxxx'
        })
      }
    }))
  }
  </code></pre>
  
  <p>Sending unique mail :</p>
  
  <pre><code>async function sendEmail (mail) { 
    // mail is an object {from, to, subject, text, html}
    const transport = setMailTransport()
    try {
      await transport.sendMail(mail)
      await transport.close()
      return 1
    } catch (err) {
      console.log(err)
      await transport.close()
      return 0
    }
  }
  </code></pre>
  
  <p>Recursive async/await function to wait mail be sent before sending a new one :</p>
  
  <pre><code>async function sendAlerts (mails, index, numberOfMailSent) {
    // mails is an array of mail object, index start at 0  
    // numberOfMailSent is just a counter to know how many mails have been sent
    if (index &gt;= mails.length) return numberOfMailSent
    const mail = mails[position]
    const newMailSent = await sendEmail(mail)
    return sendAlerts(mails, index + 1, numberOfMailSent + newMailSent)
  }
  </code></pre>
  
  <p>Any idea of where I could have been wrong or on any other way to send more than 100 mails ? </p>