How to send an email with Nodemailer using a Gmail account in Node.js?

Manoj Singh
AxleWeb Technologies
3 min readApr 26, 2022

Let’s learn how to send an email from node.js package,this is supper easy.

So, Let’s Get Started

1.Create a folder in which you want to handle all files .Ex: SEND_MAIL_NODEJS (i am using this folder name ,you can give any name)

2.Open that folder in visual studio code.

3.I hope node.js already installed in your system(windows or Mac),if not here is a good resource How to Install Node.js and NPM on Windows and Mac,please follow.

4.Open terminal in vs and type

  • Mac- $ npm init
  • Windows - npm init

just hit enter and don’t make any modification in options and in the endtype yes.

It will create a package.json file

5.install Dependencies.

as you can see that in package.json file nodemailer version is visible

6.Create a file mail.js inside SEND_MAIL_NODEJS folder

7.Here is code which you need to paste in mail.js file .

8.Before running the code you need an app password(we need to take this app password from sender email)

9. Go to your google account click on manage your Google account.

10. Click on security option.

11. 2 step verification should be on ,if not then please turn it on first ,then only you will be able to take app password.

After turning on 2 step verification ,you can see this app password option.

Select Other as app and give any name Ex :“demo”

After that click on GENERATE button to get app password .Get app password and store it some where for use it later.

12.Let’s come back to coding part again

Here enter the email id from which you are taking app password(sender email).

Here paste the app password .

Enter the sender gmail id again and receiver email id ,and give any subject of your email ex: Nodemailer is working.

Just run your code and check either you receive email or not .

Congratulations 🥳it’s working .

--

--