Error: connect ECONNREFUSED 127.0.0.1:465 nodemailer
Stefan Bogdanescu
Founder & Senior Architect
Decoding Error: connect ECONNREFUSED 127.0.0.1:465 in Nodemailer: Bridging the Gap Between Gmail and Google Workspace SMTP
As a senior developer, I’ve seen countless issues arise when integrating third-party services like Nodemailer with major email providers. The error you are encountering—Error: connect ECONNREFUSED 127.0.0.1:465—is frustratingly generic, but it points directly to a fundamental communication breakdown between your Node.js application and the mail server.
This post will dissect what this error implies in the context of using Nodemailer with Gmail/Google Workspace accounts, explain why you are facing this issue after switching domains, and provide the robust solution for reliable email delivery.
Understanding the ECONNREFUSED Error
The error ECONNREFUSED (Connection Refused) means that your application successfully attempted to establish a network connection to the specified address (127.0.0.1:465), but the target machine actively refused the connection. In simpler terms, the server at that port is not listening for connections or is actively rejecting the attempt.
When dealing with SMTP (Simple Mail Transfer Protocol) over SSL/TLS (port 465), this refusal almost always indicates one of three things:
- Authentication Failure: The credentials provided are rejected by the server.
- Server Configuration Block: The server is configured to reject connections from your specific application or IP address due to security policies.
- Service Restriction: The service provider (Google, in this case) has blocked the connection attempt because it detects an insecure or unauthorized login method.
The Gmail vs. Google Workspace Problem
Your experience perfectly illustrates a common pitfall when migrating from personal accounts to managed domain accounts: security protocol changes.
When you use a standard personal Gmail account (gmail.com), the authentication process is relatively straightforward. However, when you switch to a Google Workspace account (user@mydomain.com), you are dealing with enterprise-level security measures that strictly enforce stricter access controls.
The fact that your code works for @gmail.com but fails for @mydomain.com strongly suggests that Google has tightened the security requirements for external SMTP connections, effectively blocking the default username/password combination you are using in Nodemailer. Simply changing the account settings on the Google Console often isn't enough because the method of authentication needs to be explicitly updated for external applications.
The Best Practice Solution: App Passwords
Relying on standard user passwords for application-level SMTP access is no longer a recommended or supported method by major providers like Google. The most secure and reliable way to authenticate an external application with a Google account, especially in a server environment, is by using App Passwords.
Why Use App Passwords?
App Passwords allow you to generate unique, one-time passwords specifically for an application. This method bypasses the need to expose your main account password directly and satisfies modern security protocols, which is crucial when dealing with services that enforce stricter API and SMTP access rules.
Implementing the Fix in Nodemailer
Instead of using your regular email password, you must generate an App Password from your Google Account Security settings and use that generated password in your Nodemailer configuration.
Here is how you should structure your setup:
var nodemailer = require("nodemailer");
// IMPORTANT: Use the generated App Password here, not your main account password.
var smtpTransport = nodemailer.createTransport("SMTP",{
service: "Gmail",
auth: {
user: "user@mydomain.com", // Your full Google Workspace email address
pass: "YOUR_GENERATED_APP_PASSWORD" // <-- Use the specific App Password here
}
});
// ... rest of your mailOptions setup remains the same
smtpTransport.sendMail(mailOptions, function(error, response){
if(error){
// This should now be much clearer if authentication is successful,
// or it will throw a different error if permissions are truly denied.
console.log("Error sending mail:", error);
} else {
console.log("Message sent successfully: " + response.message);
}
});
Conclusion and Final Advice
The ECONNREFUSED error was a symptom of an outdated or insufficient authentication method colliding with modern security protocols enforced by Google for Workspace accounts. By switching to App Passwords, you are implementing the secure, intended way to authorize external applications to send mail through your Google account.
For building robust backend systems—whether using Node.js, PHP, or frameworks like Laravel where service integration is key—always prioritize secure authentication methods over relying on simple password exchange. As you build scalable applications, adopting these best practices ensures that your services remain reliable and compliant. For more insights into building reliable and structured backend solutions, explore the resources available at https://laravelcompany.com.
Note: Blog content is currently available in English.