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.
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?
Excellent. You explained the entire process like a pro!
good tutorial 👍
Nice ❤
Source code ?
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
Thanks a lot, I was stuck with OAuth from 2 days xD
I follow this process, and my logs show successful email sent, but the email never reaches my recipients inbox
Error by now
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 !💌
tysm
thanks bro really a very good explanation , please keep making such videos
helped a lot thankyou
thanks
Thanks a lot, I really apprecite that you went us through the process behind as well. Kudos 🙂
Kon kon video dekhnay say pehlay comments check krta hai?🤔🤔🤔🤔
Your explaination is good. Every one can say how to send using mailinator, but you are explaining about port number. It's great.
great video! helped me a ton keep it up !
thanks a lot Man,
thank you!