Fix Nodemailer ENETUNREACH Errors with Office 365 SMTP
Question details
The user needs to resolve intermittent connection failures when sending automated emails via Nodemailer using the Office 365 SMTP server.

- Product
- Microsoft Office 365 SMTP / Node.js
- Device & OS
- not provided
- Scenario
- Sending emails through Node.js Nodemailer via smtp.office365.com on port 587.
- Observed behavior
- Approximately 15% of outgoing messages fail with an ENETUNREACH error, typically when attempting to connect to an IPv6 address, while the remaining messages send successfully.
Ensure your Node.js hosting environment has a stable internet connection and verify that your network firewall allows outbound TCP traffic on port 587.
Force IPv4 Resolution in Nodemailer Configuration
Because the ENETUNREACH error frequently occurs due to unstable IPv6 routing to Microsoft's load balancers, forcing Node.js to connect via IPv4 can immediately stabilize the connection.
Microsoft 365 resolves to multiple IP addresses (both IPv4 and IPv6). If your server lacks proper IPv6 routing, intermittent failures will occur whenever DNS resolves to an IPv6 address.
Locate the file in your Node.js project where the Nodemailer transporter is defined.
In the configuration object where you set host to 'smtp.office365.com' and port to 587, add a new property: 'family: 4'. This tells the underlying socket to only use IPv4.
Save the changes and restart your Node.js application process. Monitor your application logs to confirm that the intermittent ENETUNREACH errors have stopped.

Verify Server Firewall and DNS Settings
If forcing IPv4 does not completely resolve the issue, network-level restrictions or unstable DNS resolution might be dropping packets.
Looking for a Lightweight Office Suite Alternative?
If you are encountering complex network restrictions or configuration problems with Microsoft Office 365 services, consider switching to WPS Office for your local document needs. It provides a lightweight, highly compatible alternative that seamlessly handles your daily productivity tasks without requiring complex cloud setups.
- 1. Download the software: Visit the official WPS Office website and download the free installer for your operating system.
- 2. Install WPS Office: Run the setup file and follow the simple on-screen instructions to complete the installation.
- 3. Open existing documents: Launch WPS Office and open your existing Microsoft Office files directly with perfect formatting retention.

Frequently Asked Questions
What does the ENETUNREACH error mean in Node.js?
ENETUNREACH stands for 'Error Network Unreachable'. It means your application attempted to connect to the SMTP server, but the operating system could not find a valid network route to reach that specific IP address. In Office 365, this usually happens when trying to connect via an unconfigured IPv6 route.
Why do my Office 365 SMTP emails succeed most of the time but fail randomly?
Microsoft's Office 365 SMTP server uses DNS round-robin load balancers that resolve to multiple IP addresses, containing a mix of IPv4 and IPv6. If your server's IPv6 routing is unstable, only the connections assigned to the faulty IPv6 path will fail, resulting in an intermittent success rate.
Can I use port 465 instead of 587 for Office 365 SMTP in Nodemailer?
No. Microsoft 365 officially requires port 587 with STARTTLS for SMTP client submission. Port 465 (Implicit SSL) is generally not supported for standard Office 365 SMTP connections and will result in connection timeouts or authentication failures.




