Sending Email via Gmail SMTP Server in NodeJS

Sending emails via SMTP server is a popular choice nowadays. It may happen your emails are landing into the spam or emails not working on your server. In both cases, use the SMTP server. When you send email through the SMTP server, there is a high possibility that your email will go straight into the user’s inbox.

Another benefit of using the SMTP server is one can send emails from the local server. Doing so, you can test and fix email content on the local server itself. It is a recommended way for developers. In this article, I show you how to send an email via Gmail SMTP server in NodeJS.

There are several NodeJS libraries available that make development easy and reliable. One of them is Nodemailer which I am going to use for this tutorial. Before proceeding, make sure you have NodeJS installed on your system as we will require to run npm commands.

In order to use the Gmail SMTP server, you need to change Gmail settings. It is mandatory if you wish to use the Gmail service for sending the emails.

Login to your Google account and click on My Account. Once you are on My Account page click on Security. Scroll down to the bottom and you will find ‘Less secure app access’ settings. Set it to ON.


Send Email via Gmail SMTP Server in NodeJS

As I said we are going to use the nodemailer library, let’s first install it. Open the terminal in the root directory of your project and run the command:

npm install --save nodemailer

Above command installs the nodemailer library which you can see under your ‘node_modules’ directory. Next, open your JS file in the editor for writing the code. Let’s say file name is app.js, add below code in it.


"use strict";
const nodemailer = require("nodemailer");

async function main() {

    let transporter = nodemailer.createTransport({
        host: '', // Gmail Host
        port: 465, // Port
        secure: true, // this is true as port is 465
        auth: {
            user: 'GMAIL_USERNAME', // generated ethereal user
            pass: 'GMAIL_PASSWORD', // generated ethereal password

    // send mail with defined transport object
    let info = await transporter.sendMail({
        from: '"FROM_NAME" <FROM_EMAIL_ADDRESS>', // sender address
        to: "RECEPIENT_EMAIL_ADDRESS", // list of receivers
        subject: "Welcome Email", // Subject line
        //text: "Hello world?", // plain text body
        html: "This email is sent through <b>GMAIL SMTP SERVER</b>", // html body

    console.log("Message sent: %s", info.messageId);


Make sure to replace placeholders with the actual values. Here I am using the HTML content. If the user wants to send a plain text then pass the content to the ‘text’ key. Run the above code in the terminal using the command:

node app.js

Upon sending email successfully, you should see output something like below:


That’s it! I hope you understand how to send an email via Gmail SMTP server in NodeJS. Please share your thoughts and suggestions in the comment section below.

Related Articles

If you liked this article, then please subscribe to our YouTube Channel for video tutorials.

4 thoughts on “Sending Email via Gmail SMTP Server in NodeJS

  1. Really a piece of good information you have shared which is very helpful for beginners of the technology management people. Your articles are always very helpful and in a language that even non-technical people can also understand what is the information all about. Thank you for sharing a rich article.

    1. i tried and it did not work for me.

      also this worked for me:

      port: 587, secure: false,

      this website suggests to only use port 587 and not 465:

      maybe “less secure access app” can be left off to disable less secure apps?

      and per documentation: “If false (the default) then TLS is used if server supports the STARTTLS extension.” – so i suppose its ok to use “secure:false” but it feels counterintuitive.

Leave a Reply

Your email address will not be published. Required fields are marked *