1
votes

Im trying to use OAUTH 2.0 to send emails with Gmail API using Nodemailer

I have Enable the Gmail API and i have clientId, clientSecret, refreshToken and accessToken (refreshtoken and accesstoken were taken from developers.google.com/oauthplayground) because these all were needed for OAUTH to work.

i have followed every detail form (https://nodemailer.com/smtp/oauth2/#examples) But i'm getting following error

{ Error: connect ETIMEDOUT 74.125.140.108:465
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1097:14)
  errno: 'ETIMEDOUT',
  code: 'ESOCKET',
  syscall: 'connect',
  address: '74.125.140.108',
  port: 465,
  command: 'CONN' }

But there is a catch when i use Plain username and password there is no connection timeout error because i have already enabled less secure app access form (https://myaccount.google.com/lesssecureapps) so there is no error in sending emails using plain username and password but this is not a good approach so OAUTH is the solution

here is my code for you to check (its an express app) running in development running on http://localhost:3000

var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 465,
secure: true,
service: 'gmail',
auth: {
  type: 'OAuth2',
  user: '[email protected]',
  clientId: 'clientID',
  clientSecret: 'clientSecret',
  refreshToken: 'refresh token obtained from https://developers.google.com/oauthplayground',
  accessToken: 'access token also obtained from https://developers.google.com/oauthplayground'
}
});
var mailOptions = {
    from: 'me <[email protected]>',
    to: '',
    subject: 'Subject ',
    text: 'Some thing',
  };

  transporter.sendMail(mailOptions, (error, response) => {
    if(error){
        console.log(error);   // Always giving Error: connect ETIMEDOUT 74.125.140.108:465
    } else {
        console.log(response);
    }
});

it not even try to login because when login fails it gives error of wrong credentials (checked with plain username and password)

*Note please don't mark this question as duplicate because i have check almost all other question and answers but nothing worked some people said its because of firewall setting i have check that too but didn't worked So whats the issue and how to solve it

2

2 Answers

0
votes

After looking to your problem i assume that when generated the OAuth credentials e.g(clienId and Clientsecret ) you did not added https://mail.google.com/ in you scope on connect screen because you would need verification form google to add these scope for the credentials

you must have added this to your scope on (https://developers.google.com/oauthplayground) but i'm sure you have add this to your scope on consent screen as well before generating credentials

OAuth consent screen

as you are in Development mode and running your application on localhost so you don't have private domain and privacy policy

Adding https://mail.google.com to your scope before generating clientId and Cliensecret will resolve your problem

0
votes

Your port no is wrong for gmail. Use This Format for gmail

const nodeMailer = require('nodemailer');

let transporter = nodeMailer.createTransport({
    host: "smtp.gmail.com",
    port: 587,
    secure: false, // true for 587, false for other ports
    requireTLS: true,
    auth: {
        user: your email, 
        pass: your password, 
    },
});

let mailOptions = {
    from: '[email protected]',
    to: '[email protected]',
    subject: 'Sending Email using Node.js',
    text: 'That was easy!'
};

transporter.sendMail(mailOptions, function(error, info){
    if (error) {
       console.log(error);
    } else {
        console.log('Email sent: ' + info.response);
    }
});