2026-07-15

Using SMTP, Gmail, and STARTTLS

Stefan Bogdanescu

Stefan Bogdanescu

Founder & Senior Architect

Using SMTP, Gmail, and STARTTLS

Mastering SMTP, Gmail, and STARTTLS: Troubleshooting Your Email Sending Dilemma

As developers, we often encounter networking and protocol challenges when dealing with services like email. Understanding the intricacies of the Simple Mail Transfer Protocol (SMTP), secure connections like STARTTLS, and how these apply to major providers like Gmail can be surprisingly complex. Many users successfully receive mail but struggle immensely with reliably sending mail, especially when security protocols are involved.

This post dives deep into the specific issues you're facing when trying to establish an outgoing connection via telnet to smtp.gmail.com, and provides a developer-focused breakdown of the correct protocol flow and troubleshooting steps.

The SMTP Workflow: Sending vs. Receiving

The core confusion often lies in distinguishing between receiving mail (which is usually handled by incoming server connections) and sending mail (which requires an authenticated submission process).

When you receive mail, your email client connects to a receiving server, which expects standard SMTP commands (HELO, MAIL FROM, RCPT TO). When you send mail, your application acts as a client submitting the message to an outgoing server. This submission process requires authentication and encryption.

The typical flow for sending mail is: Your Application (Client) $\rightarrow$ SMTP Server (e.g., Gmail's Outgoing Server) $\rightarrow$ Recipient's SMTP Server.

Decoding Ports: 465 vs. 587 and STARTTLS

You mentioned struggling with port 465 and the requirement for STARTTLS. This distinction is crucial in modern email transport:

  1. Port 465 (SMTPS): Historically, this port was often used to establish an implicit SSL/TLS connection immediately upon connection (SMTPS). While some systems still use it, it relies on the client initiating the secure handshake before sending complex SMTP commands.
  2. Port 587 (Submission): This is the modern, preferred standard for submitting mail. When connecting to port 587, the server expects you to initiate the connection with HELO first, and then issue the STARTTLS command to upgrade the plain text connection to a secure TLS connection.

The reason you experienced an immediate disconnection after sending HELO on port 465 is likely due to a mismatch in how Gmail's server expects the initial handshake when using that specific port, or perhaps an issue with your local network configuration interfering with the raw telnet session.

Troubleshooting the Connection and Network Issues

If you are attempting this via a simple telnet session for testing, remember that these tools test raw TCP connections without handling complex SMTP state management perfectly. For reliable application development, always use dedicated libraries or HTTP clients rather than pure telnet.

Best Practice: Use Explicit Submission (Port 587)

For modern applications, using port 587 with explicit STARTTLS is generally more robust for sending mail through providers like Gmail:

$ telnet smtp.gmail.com 587
Trying 142.250.191.46...
Connected to smtp.gmail.com.
...
HELO myapp.com
STARTTLS
... (Followed by the TLS negotiation)

If this still fails, the issue is likely related to authentication or firewall rules on your specific network, rather than the protocol itself. We often see similar networking hurdles when dealing with external APIs and services; robust architectural design, much like in frameworks such as Laravel, relies on correctly handling these external communication boundaries securely.

Addressing the Side Note: Network Connectivity

Your side note regarding the "No route to host" error is a separate, critical infrastructure issue. This error indicates that your device cannot establish a valid network path to the destination IP address, regardless of the protocol being used.

This usually points to one of three things:

  1. Firewall Restriction: A firewall (either on your machine or your local Wi-Fi router) is blocking outbound connections on certain ports.
  2. ISP Routing Issue: The specific path your Internet Service Provider (ISP) uses to reach that external server is congested or misconfigured.
  3. VPN/Proxy Conflict: If you are using a VPN or proxy, it might be interfering with raw TCP sessions like telnet.

If you are unable to connect directly via telnet, ensure you are testing the connection from an environment where outbound traffic is unrestricted, such as a remote server (as you correctly found when SSHing into your school's box). This helps isolate whether the problem lies with the protocol or the local network path.

Conclusion

Sending email securely requires mastering the nuances of SMTP negotiation, port selection, and security protocols like STARTTLS. While raw tools like telnet are useful for debugging basic connectivity, real-world application development demands robust HTTP-based libraries to handle authentication and encryption properly. By understanding the difference between port 465 and 587, and by addressing underlying network issues, you can successfully manage your email communications. For building secure, scalable applications that interface with services like email delivery, adopting established architectural patterns is key; this approach echoes the principles of strong design found in frameworks like Laravel.

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.