How to Send Email using Gmail Account in 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...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Get Client IP Address in Node JS?
- How to Render HTML File in Node JS Express?
- How to Create Separate Routes File in Node JS Express?
- How to Get Query String Value in Node.js?
- Node JS Express Route with Parameters Example
- How to Create Route in Node JS Express JS?
- How to Generate 4,6,8,10 Digit Random number in Node JS?
- Node JS Read and Write Json File Example
- Node JS Find and Update Object in Array Example
- Node JS Make HTTP Delete Request Example
- Node JS Express Form Submission Example
- Node JS Convert Image File to Base64 String Example