Step-by-Step Tutorial: Sending Emails in Node.js using Nodemailer & Gmail

Posted by


Introduction:
In this tutorial, we will learn how to send emails in Node.js using the Nodemailer library and Gmail SMTP server. Nodemailer is a popular Node.js module for sending emails, and Gmail provides a simple and reliable SMTP server for sending emails. By the end of this tutorial, you will be able to send emails programmatically using Node.js and Gmail.

Step 1: Setting up a Gmail account
Before we start, create a new Gmail account if you don’t have one already. This account will be used to send emails using the Gmail SMTP server. Make sure to enable "Less secure app access" for this account by going to https://myaccount.google.com/security and turning on the option.

Step 2: Creating a new Node.js project
Create a new directory for your Node.js project and navigate into it using the terminal. Run the following command to create a new package.json file:

npm init -y

Step 3: Installing Nodemailer
Next, install the Nodemailer library in your project using the following command:

npm install nodemailer

Step 4: Creating a new JavaScript file
Create a new JavaScript file in your project directory and name it sendEmail.js. This file will contain the code to send emails using Nodemailer and Gmail.

Step 5: Writing the code to send emails
Open the sendEmail.js file in your favorite text editor and add the following code:

const nodemailer = require('nodemailer');

// Create a transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
    service: 'gmail',
    auth: {
        user: 'your_email@gmail.com',
        pass: 'your_password'
    }
});

// Setup email data
let mailOptions = {
    from: 'your_email@gmail.com',
    to: 'recipient_email@gmail.com',
    subject: 'Test Email',
    text: 'Hello, this is a test email!'
};

// Send email
transporter.sendMail(mailOptions, (error, info) => {
    if (error) {
        return console.log(error);
    }
    console.log('Message sent: %s', info.messageId);
});

Make sure to replace ‘your_email@gmail.com’ and ‘your_password’ with your Gmail account email address and password respectively. Also, replace ‘recipient_email@gmail.com’ with the email address of the recipient you want to send the email to.

Step 6: Running the code
Save the sendEmail.js file and run it using the following command:

node sendEmail.js

If everything is set up correctly, you should see a message in the console indicating that the email has been sent successfully.

Conclusion:
In this tutorial, we have learned how to send emails in Node.js using the Nodemailer library and Gmail SMTP server. Sending emails programmatically can be very useful for various applications, such as sending notifications, alerts, and even marketing emails. Nodemailer makes it easy to send emails in Node.js, and Gmail provides a reliable SMTP server for sending emails. I hope this tutorial was helpful, and you can now start sending emails in your Node.js applications.

0 0 votes
Article Rating
33 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@shakhawathhussainkayes5241
2 months ago

i am getting this error …. Error: connect ETIMEDOUT 74.125.130.109:465

at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1607:16) {

errno: -4039,

code: 'ESOCKET',

syscall: 'connect',

address: '74.125.130.109',

port: 465,

command: 'CONN'

} any solution?

@marvinotieno2194
2 months ago

Excellent. You explained the entire process like a pro!

@siddhantota2224
2 months ago

good tutorial 👍

@anirbanbhunia3363
2 months ago

Nice ❤

@kshitijmalode7671
2 months ago

Source code ?

@user-nq2lh1lb7u
2 months ago

If we want to send the email to that user's email which he or she would provide to the Bot. What would be the procedure of this? Kindly Explain

@sza619
2 months ago

Thanks a lot, I was stuck with OAuth from 2 days xD

@cowboyleal3148
2 months ago

I follow this process, and my logs show successful email sent, but the email never reaches my recipients inbox

@arabeyabelajarnahwu-shorof5902
2 months ago

Error by now

@pkflims1018
2 months ago

thanks my app was already working but i am also aware about gogle policy but not getting exact how to genrate app password and it works now !💌

@fiascogamingchannel78
2 months ago

tysm

@Strange_r53626
2 months ago

thanks bro really a very good explanation , please keep making such videos

@mukulagrawal3512
2 months ago

helped a lot thankyou

@user-fi6bu7cp5e
2 months ago

thanks

@recipeFor
2 months ago

Thanks a lot, I really apprecite that you went us through the process behind as well. Kudos 🙂

@MuhammadRafay-we9nc
2 months ago

Kon kon video dekhnay say pehlay comments check krta hai?🤔🤔🤔🤔

@dubaisheikh99
2 months ago

Your explaination is good. Every one can say how to send using mailinator, but you are explaining about port number. It's great.

@Luniy5_
2 months ago

great video! helped me a ton keep it up !

@sathishs8728
2 months ago

thanks a lot Man,

@karennino6639
2 months ago

thank you!