How to Send Email using Gmail Account in Node.js?

By Hardik Savani August 24, 2021 Category : Node JS

Hello,

This post will give you example of node js send email example. I’m going to show you about node js send email through gmail. this example will help you node.js send email with nodemailer. you can understand a concept of how to send a mail using node js.

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...

Tags :
Shares