2026-07-15

Could not connect to SMTP host: smtp.gmail.com, port: 587; nested exception is: java.net.ConnectException: Connection timed out: connect

Stefan Bogdanescu

Stefan Bogdanescu

Founder & Senior Architect

Could not connect to SMTP host: smtp.gmail.com, port: 587; nested exception is: java.net.ConnectException: Connection timed out: connect

Decoding the Silence: Troubleshooting SMTP Connection Timeouts with Gmail

As a senior developer, you know that debugging network issues can often feel like staring into a black box. When an application attempts to connect to an external service, and it results in a cryptic error like java.net.ConnectException: Connection timed out: connect, the immediate instinct is to check firewalls and ports. However, as we’ve seen with email services like Gmail, the problem often lies deeper within authentication protocols, security configurations, or specific server restrictions rather than simple network blockage.

This post will walk you through diagnosing why your JavaMail implementation fails to connect to smtp.gmail.com:587, and provide the modern, secure solutions required to establish reliable email delivery.

Understanding the Connection Timeout Error

The error Connection timed out indicates that your client (your Java application) sent a request to the SMTP server, but it received no response within the expected timeframe. This typically suggests one of three things:

  1. Network Blockage: A physical firewall or router is silently dropping the connection attempt.
  2. Server Rejection: The target server (smtp.gmail.com) received the request but intentionally failed to respond, often due to strict security policies that block unauthenticated or improperly authenticated connections.
  3. Protocol Mismatch: The handshake required for the secure connection (like STARTTLS) is failing before successful authentication can occur.

Since you have already verified physical network access (ping works), we must focus on points 2 and 3, which relate directly to how modern email providers handle external application access.

The Gmail SMTP Security Hurdle

The steps you took—checking the firewall and enabling "Less Secure Apps"—are the traditional fixes. However, Google has deprecated these older methods due to evolving security standards. For applications using standard protocols like SMTP (port 587 with STARTTLS), modern authentication requires a more robust method than a simple username and password.

The connection timeout is frequently a symptom of the server blocking the initial connection attempt because it detects an insecure or insufficient authentication attempt, leading to no response being sent back before the client times out.

The Modern Solution: App Passwords and OAuth2

To reliably connect external applications (like your Java code) to a Gmail account, you must use Google’s modern security framework. This involves generating an App Password instead of using your regular account password.

  1. Enable 2-Step Verification: Ensure you have Two-Factor Authentication enabled on your Google account.
  2. Generate an App Password: Go to your Google Account Security settings and generate a specific, unique password for this application. This password can be used in place of your main login credentials when connecting via external clients.

Using an App Password ensures that the connection is authenticated correctly according to modern OAuth standards, allowing the SMTP server to successfully establish the secure TLS tunnel without timing out.

Reviewing Your Java Implementation

The Java code you provided demonstrates a standard setup using javax.mail:

properties.put("mail.smtp.host", host); // smtp.gmail.com
properties.put("mail.smtp.port", port); // 587
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true"); // Crucial for secure connection

// ... Session and Transport setup

This code correctly sets up the required parameters for an SMTP connection using STARTTLS, which is essential for security. The issue was not in the structure of the JavaMail setup, but rather in the credentials being presented to the external server. When dealing with services that enforce strict OAuth (like Google), the credentials themselves must be validated according to those modern rules.

Conclusion: Building Robust Systems

Troubleshooting these kinds of network and application-layer errors teaches us a vital lesson: infrastructure security is not just about firewalls; it’s about understanding the protocols and authentication mechanisms enforced by the services we rely on. Whether you are building a complex backend system, perhaps leveraging frameworks like Laravel for robust API design, ensuring that your service layer can reliably interact with external dependencies is paramount.

For reliable application development, always prioritize using the latest security standards provided by your service providers. By switching to App Passwords and understanding the nuances of TLS negotiation, you move from guessing fixes to implementing secure, sustainable solutions.

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.