For those looking for a working example / code snippet, follow the Radioreve Answer until you can get the access token and update the token. (Basically, go to the playground, make sure it requests access to send mail and mail.google.com, gives permission, an exchange authorization code for tokens)
Please note that the expires time expires was new Date().getTime() + 2000 , which was close to the seconds indicated on the playing court. I'm not sure if I should enter the access token and the expiration time exactly, as it seems to update the token automatically.
Use this sample code written in ECMAScript 6:
const user_name = 'something@gmail.com'; const refresh_token = ''; const access_token = ''; const client_id = ''; const client_secret = ''; const email_to = 'receiver@gmail.com'; const nodemailer = require('nodemailer'); let transporter = nodemailer .createTransport({ service: 'Gmail', auth: { type: 'OAuth2', clientId: client_id, clientSecret: client_secret } }); transporter.on('token', token => { console.log('A new access token was generated'); console.log('User: %s', token.user); console.log('Access Token: %s', token.accessToken); console.log('Expires: %s', new Date(token.expires)); }); // setup e-mail data with unicode symbols let mailOptions = { from : user_name, // sender address to : email_to, // list of receivers subject : 'Hello ✔', // Subject line text : 'Hello world ?', // plaintext body html : '<b>Hello world ?</b>', // html body auth : { user : user_name, refreshToken : refresh_token, accessToken : access_token, expires : 1494388182480 } }; // send mail with defined transport object transporter.sendMail(mailOptions, function (error, info) { if (error) { return console.log(error); } console.log('Message sent: ' + info.response); });
Logan May 11 '17 at 7:12 2017-05-11 07:12
source share