Node JS Send Email using Gmail Account

August 24, 2021 | Category : Other

Hello,In this tutorial we will go over the demonstration of how to send a mail using node js. I’m going to show you about how to send email using google gmail in node js. this example will help you node js send email example. we will help you to give example of node js send email through gmail. Alright, let’s dive into the steps.

In this example we will use nodemailer npm package for sending email. here we will use sender as google gmail account and you have to add your email and password in sender details.

so let's follow simple step to send mail with node js.

Step 1: Create Node App

run bellow command and create node app.

mkdir my-app

cd my-app

npm init

Step 2: Install nodemailer

run bellow command and install nodemailer npm package.

npm install nodemailer -S

Step 3: Create server.js file

server.js

const nodemailer = require('nodemailer');

let transporter = nodemailer.createTransport({

service: 'gmail',

auth: {

user: "your_gmail_account@gmail.com",

pass: "PASSWORD"

}

});

let message = {

from: "from@email.com",

to: "receiver_email@gmail.com",

subject: "Subject",

html: "<h1>Hello SMTP Email</h1>"

}

transporter.sendMail(message, function(err, info) {

if (err) {

console.log(err);

} else {

console.log(info);

}

})

now you can simply run by following command:

node server.js

Output:

i hope it can help you...