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

Leave a Reply

33 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@shakhawathhussainkayes5241
25 days 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
25 days ago

Excellent. You explained the entire process like a pro!

@siddhantota2224
25 days ago

good tutorial 👍

@anirbanbhunia3363
25 days ago

Nice ❤

@kshitijmalode7671
25 days ago

Source code ?

@user-nq2lh1lb7u
25 days 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
25 days ago

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

@cowboyleal3148
25 days ago

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

@arabeyabelajarnahwu-shorof5902
25 days ago

Error by now

@pkflims1018
25 days 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
25 days ago

tysm

@Strange_r53626
25 days ago

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

@mukulagrawal3512
25 days ago

helped a lot thankyou

@user-fi6bu7cp5e
25 days ago

thanks

@recipeFor
25 days ago

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

@MuhammadRafay-we9nc
25 days ago

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

@dubaisheikh99
25 days 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_
25 days ago

great video! helped me a ton keep it up !

@sathishs8728
25 days ago

thanks a lot Man,

@karennino6639
25 days ago

thank you!

33
0
Would love your thoughts, please comment.x
()
x