Complete Guide to Sending Emails via Gmail Using Basic SMTP Commands

Nov 30, 2025 · Programming · 14 views · 7.8

Keywords: SMTP | Gmail | OpenSSL | TLS | Authentication

Abstract: This article provides a comprehensive guide on using fundamental SMTP commands to send emails through Gmail servers, with emphasis on overcoming TLS encryption and authentication challenges. It demonstrates secure connection establishment using OpenSSL, detailed usage of core SMTP commands including EHLO, AUTH PLAIN, MAIL FROM, RCPT TO, and DATA, along with complete operational examples and Base64 encoding explanations.

SMTP Protocol Fundamentals and Gmail's Special Requirements

The Simple Mail Transfer Protocol (SMTP) serves as the core standard for email transmission across the internet. In basic SMTP sessions, clients communicate with servers through a series of text commands to complete the email sending process. Typical SMTP sessions include connection establishment, handshake, authentication, and mail transmission phases.

As a globally widely used email service provider, Gmail imposes strict security requirements on its SMTP servers. Unlike traditional local SMTP servers, Gmail mandates that all connections must use Transport Layer Security (TLS) encryption and require rigorous authentication. While this security mechanism enhances email transmission safety, it also increases the technical complexity of direct operation through basic commands.

Establishing Secure Connections Using OpenSSL

Since standard telnet tools do not support TLS encryption, they cannot directly connect to Gmail's SMTP servers. OpenSSL provides an effective solution to this problem, enabling encrypted SMTP connections while maintaining interactive operation similar to telnet.

Gmail offers two main SMTP connection methods: port 587 supports the STARTTLS command, while port 465 directly uses SSL encryption. Below are detailed explanations of both connection approaches:

For the STARTTLS approach, use the command: openssl s_client -starttls smtp -connect smtp.gmail.com:587 -crlf -ign_eof

Parameter explanation: -starttls smtp specifies using SMTP's STARTTLS extension, -connect defines the target server and port for connection, -crlf ensures proper line ending handling, -ign_eof keeps the connection from closing immediately after input ends.

For direct SSL connection approach, use the command: openssl s_client -connect smtp.gmail.com:465 -crlf -ign_eof

This method establishes SSL encrypted connection directly, omitting the STARTTLS negotiation process, which may be more stable in certain network environments.

Detailed Explanation of SMTP Session Commands

After successfully establishing an encrypted connection, the server returns a welcome message, such as: 220 mx.google.com ESMTP m46sm11546481eeh.9

At this point, the formal SMTP session process can begin:

EHLO Command: Extended Hello command, used to declare client identity to the server and obtain the list of features supported by the server.

Example: EHLO localhost

Server response typically includes supported authentication methods, maximum mail size limits, and other information:

250-mx.google.com at your service, [1.2.3.4] 250-SIZE 35882577 250-8BITMIME 250-AUTH LOGIN PLAIN XOAUTH 250 ENHANCEDSTATUSCODES

AUTH PLAIN Authentication: Gmail supports multiple authentication mechanisms, with AUTH PLAIN being the most straightforward approach. This method requires sending username and password encoded in Base64 in a single transmission.

Encoding format: null character + username + null character + password

Using command line to generate Base64 encoding: echo -ne '\00user@gmail.com\00password' | base64

The resulting encoding appears as: AHVzZXJAZ21haWwuY29tAHBhc3N3b3Jk

Complete authentication command: AUTH PLAIN AG5pY2UudHJ5QGdtYWlsLmNvbSBub2l0c25vdG15cGFzc3dvcmQ=

After successful authentication, server returns: 235 2.7.0 Accepted

Email Sending Process

After completing authentication, proceed to the standard email sending process:

MAIL FROM Command: Specifies the sender address.

Example: MAIL FROM: <gryphius-demo@gmail.com>

Server confirmation: 250 2.1.0 OK m46sm11546481eeh.9

RCPT TO Command: Specifies the recipient address, can be used multiple times to add multiple recipients.

Example: RCPT TO: <somepoorguy@example.com>

Server confirmation: 250 2.1.5 OK m46sm11546481eeh.9

DATA Command: Begins email content transmission.

After entering DATA, server responds: 354 Go ahead m46sm11546481eeh.9

At this point, email headers and body content can be entered, formatted as follows:

Subject: it works This is the email body content. Adding more lines as needed.

Email content ends with a single line containing only a period: .

Server confirms email receipt: 250 2.0.0 OK 1339757532 m46sm11546481eeh.9

QUIT Command: Terminates the SMTP session.

After entering QUIT, server closes the connection: 221 2.0.0 closing connection m46sm11546481eeh.9

Alternative Solutions for Windows Environment

In Windows environments where OpenSSL might not be available, consider using the stunnel tool. Stunnel can establish tunnels between ordinary TCP connections and SSL encrypted connections, enabling standard telnet clients to communicate with Gmail SMTP servers through encrypted channels.

Configuring stunnel requires setting in the configuration file:

[gmail-smtp] client = yes accept = 127.0.0.1:25 connect = smtp.gmail.com:465

After configuration, use telnet localhost 25 to connect to the local stunnel service, then follow the standard SMTP process.

Technical Points and Best Practices

When using basic SMTP commands to operate Gmail, pay attention to the following key technical points:

Base64 encoding must be accurate, especially the handling of null characters. Incorrect encoding will cause authentication failure.

TLS connection stability: Network environment may affect the establishment of encrypted connections, recommend operating in stable network conditions.

Command format strictness: SMTP protocol has strict requirements for command format, including case sensitivity, spaces, angle brackets, all must comply with specifications.

Error handling: Familiarity with common SMTP error codes, such as authentication failure, address format errors, etc., helps quickly identify issues.

Security considerations: While this method is suitable for educational and testing purposes, in production environments, it's recommended to use mature email client libraries that provide better error handling and security.

By mastering these basic SMTP commands and Gmail's special requirements, developers can deeply understand the working principles of email transmission, laying a solid foundation for subsequent development of email-related applications.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.