title: Nodemailer Error: connect ECONNREFUSED 127.0.0.1:587 (errno: -61), but I specified port: 465, secure: true
body: <p>I'm trying to use Nodemailer in Node.js with Google OAuth2, using port: 465, secure: true. But I'm receiving the error message below:</p>
  <pre><code>Error: connect ECONNREFUSED 127.0.0.1:587
  at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1137:16) {
    errno: -61,
    code: 'ESOCKET',
    syscall: 'connect',
    address: '127.0.0.1',
    port: 587,
    command: 'CONN'
  }
  </code></pre>
  <p>As you can see... Port 587 is coming in the error message, but it wasn't defined anywhere in my app.</p>
  <p>In my previous configuration, I was using Nodemailer with Node.js, port: 587, secure: false, rejectUnauthorized: false. And it worked like a charm.</p>
  <p>However, Nodemailer is defaulting to 587 for some reason. It may be my fault, but I'm declaring port: 465 with secure: true. And, according to their documentation, it only defaults to 587 if you type secure: false.</p>
  <p>Here's my configuration:</p>
  <pre><code>async function sendEmail() {
    try {
      const REDIRECT_URI = config.redirect_uri;
      const oAuth2Client = new google.auth.OAuth2(config.mailClientID, 
      config.mailClientSecret, REDIRECT_URI);
      oAuth2Client.setCredentials({refresh_token: config.refresh_token});
      const accessToken = await oAuth2Client.getAccessToken();
  
      let smtpConfigWithToken = nodemailer.createTransport({
          service: 'Gmail',
          host: 'smtp.gmail.com',
          port: 465,
          secure: true,
          auth: {
              type: 'OAuth2',
              user: config.email,
              clientId: config.mailClientID,
              clientSecret: config.mailClientSecret,
              refreshToken: config.refresh_token,
              accessToken: accessToken
          }
      });
  
      let transporter = nodemailer.createTransport(smtpConfigWithToken);
  
      let HelperOptions = {
        from: '&lt;birthmet@themetropolitanschool.com&gt;',
        to: emailRecipients,
        bcc: '&lt;lgranados@themetropolitanschool.com&gt;',
        subject: &quot;New Birthday Cake request&quot;,
        html: output
      };
  
      const result = transporter.sendMail(HelperOptions);
      return result;
    } catch(err) {
      return err;
    }
  }
  sendEmail().then(result =&gt; console.log('Email sent...', result)).catch(err =&gt; console.log(err));
  </code></pre>
  <p>I can always go back, but I'm putting a good effort into making my apps more secure. I very much prefer not to enable &quot;Less Secure Apps&quot;.
  <strong>Note</strong>: the URI is also verified by Google.</p>
  <p>As a troubleshoot step, I've run the sample code from Google and I can get the labels, as well as token.json. See this <a href="https://developers.google.com/gmail/api/quickstart/nodejs" rel="nofollow noreferrer">link</a> for Node.js code. My app also needs access to Spreadsheets, but that part is working flawlessly (using the same token credentials).</p>
  <p>Can you help me with a solution on this wrong port error?</p>
  <p>Thank you in advance for taking your time reading this.</p>