2026-07-15

Sending email via Node.js using nodemailer is not working

Stefan Bogdanescu

Stefan Bogdanescu

Founder & Senior Architect

Sending email via Node.js using nodemailer is not working

Why Your Nodemailer Emails Fail: Solving the Gmail SMTP Authentication Block

As a senior developer, I’ve seen countless scenarios where backend services seem perfectly fine in theory but fail spectacularly during execution—especially when dealing with external services like email delivery. Sending emails via Node.js using Nodemailer is a fantastic way to build robust applications, but authentication issues, particularly with services like Gmail, are notoriously tricky.

You've encountered a very common roadblock: attempting to use your personal Gmail account credentials directly within an application setup results in Google blocking the login attempt with a security warning. This isn't a bug in Nodemailer itself; it’s a fundamental change in how Google enforces security for third-party applications accessing mail via SMTP.

This post will dissect why this happens and provide the definitive, secure solution to get your Node.js email sending working reliably.

Understanding the Authentication Hurdle

You correctly identified the two main ways to send mail: sending directly (which requires complex server setup) or using an external SMTP service like Gmail. When using SMTP, you are asking a third-party server (Google’s mail servers) to relay the message for you. To do this securely, Google requires more than just your standard username and password; it demands explicit authorization for applications.

The error message you received—"sign-in attempt blocked"—is Google actively stopping unauthorized access attempts using standard credentials. This is a security feature designed to prevent malicious actors from hijacking accounts. When you use a regular password in an application context, Google flags it as insecure.

The Solution: Using App Passwords for Gmail SMTP

The solution lies in changing the authentication method. Instead of relying on your primary account password, you must generate a specific, restricted password intended only for that application. For Gmail accounts, this is achieved using App Passwords.

Step-by-Step Fix

  1. Enable 2-Factor Authentication (2FA): Ensure that 2-Factor Authentication is enabled on your Google account. This is a prerequisite for generating App Passwords.
  2. Generate an App Password: Log into your Google Account Security settings. Navigate to the App Passwords section and generate a unique, 16-character password specifically for your Node.js application. Do not use your standard Gmail password here.
  3. Update Your Nodemailer Configuration: Replace your regular password in the Nodemailer setup with this newly generated App Password.

Here is how your corrected server configuration should look:

var nodemailer = require('nodemailer');

// IMPORTANT: Use the specific App Password you generated, not your main Gmail password.
var transporter = nodemailer.createTransport("SMTP", {
    service: 'gmail', // Use 'gmail' service for Gmail SMTP
    auth: {
        user: '*my personal Gmail address*', // Your full Gmail address
        pass: '*The 16-character App Password*' // <-- Use the App Password here!
    }
});

var http = require('http');
var httpServer = http.createServer(function (request, response) {
    transporter.sendMail({
       from: '*my personal Gmail address*',
       to: '*my personal Gmail address*',
       subject: 'Hello World via Node.js!',
       text: 'This email was sent successfully using App Passwords.'
    })
    .then(info => {
        console.log('Message sent: %s', info.messageId);
        response.writeHead(200, {'Content-Type': 'text/plain'});
        response.end('Email sent successfully!');
    })
    .catch(error => {
        console.error('Error sending email:', error);
        response.writeHead(500, {'Content-Type': 'text/plain'});
        response.end('Failed to send email.');
    });
});

httpServer.listen(8080, function() {
    console.log('Server running on port 8080');
});

Best Practices for Application Security

When building applications—whether they are backend services or full-stack platforms like those built with Laravel—security must be paramount. Relying on static credentials is a major vulnerability. Always prioritize using environment variables to store sensitive keys and passwords, rather than hardcoding them in your source files. This practice ensures that if you deploy your application (perhaps using frameworks like Laravel for API management), your secrets remain secure.

By understanding the nuances of external service authentication and implementing secure methods like App Passwords, you move from fighting with error messages to building reliable, production-ready systems.

Conclusion

The issue you faced is a classic case of security protocol enforcement rather than a coding error in Nodemailer. By switching your Gmail SMTP authentication to use an App Password, you satisfy Google's security requirements, allowing your Node.js application to successfully relay emails through the service. Implement this change, secure your credentials using environment variables, and you will have a robust email sending mechanism.

Note: Blog content is currently available in English.

Tags:

Enhance your marketing setup with your own email marketing platform.

Join the growing number of SaaS platforms using Laravel Mail to offer email marketing solutions to their customers.