📅  最后修改于: 2023-12-03 15:30:59.145000             🧑  作者: Mango
SMTP (Simple Mail Transfer Protocol) is a protocol used for sending email messages between servers. Telnet is a way of connecting to a server via the command line. In combination, telnet can be used to connect to an SMTP server and send email messages manually.
In this tutorial, we will use telnet to connect to Gmail's SMTP server and send an email message through it. We will assume that you have some basic knowledge of the command line and SMTP.
To follow along with this tutorial, you will need the following:
Gmail security settings by default do not allow less secure apps to access your account. Since we will be connecting to the SMTP server via telnet, we will need to enable less secure app access in your Gmail account. To do this:
Next, we will use telnet to connect to the Gmail SMTP server on port 587. Open up your command line interface and enter the following command:
telnet smtp.gmail.com 587
You should see some output that looks like this:
Trying 74.125.195.109...
Connected to gmail-smtp-msa.l.google.com.
Escape character is '^]'.
220 smtp.gmail.com ESMTP w27sm2757712wmd.27 - gsmtp
Once you have successfully connected to the SMTP server, we need to initiate a secure connection by running the following command:
STARTTLS
You will receive a response that looks like this:
220 2.0.0 Ready to start TLS
After initiating a secure connection, we need to authenticate ourselves with our Gmail account. Enter the following command:
AUTH LOGIN
You will receive a response that looks like this:
334 VXNlcm5hbWU6
This is a Base64 encoded string that expects your email address. Enter your email address in Base64 format, then press enter. You will receive a response that looks like this:
334 UGFzc3dvcmQ6
This is another Base64 encoded string that expects your Gmail account password. Enter your password in Base64 format, then press enter. You will receive a response that looks like this:
235 2.7.0 Accepted
This indicates that you have successfully authenticated with the Gmail SMTP server.
After authentication, we need to set the email sender address. Run the following command:
MAIL FROM: <your-email-address>
Replace <your-email-address>
with your actual email address. You will receive a response that looks like this:
250 2.1.0 OK w27sm2757712wmd.27 - gsmtp
Next, we need to set the recipient email address. Run the following command:
RCPT TO: <recipient-email-address>
Replace <recipient-email-address>
with the email address of the recipient. You will receive a response that looks like this:
250 2.1.5 OK w27sm2757712wmd.27 - gsmtp
After setting the recipient email address, we can now set the email message. Run the following command:
DATA
You will receive a response that looks like this:
354 Go ahead w27sm2757712wmd.27 - gsmtp
This means that the server is ready to receive the email message. Enter the following text and press enter:
Subject: Test Email
This is a test email message sent via telnet.
.
Note that there is a period at the end of the message, which indicates the end of the message.
You will receive a response that looks like this:
250 2.0.0 OK 1598975509 w27sm2757712wmd.27 - gsmtp
This means that the email message has been sent successfully.
After sending the email message, we can quit telnet by running the following command:
QUIT
You will receive a response that looks like this:
221 2.0.0 closing connection w27sm2757712wmd.27 - gsmtp
This means that the connection has been closed successfully.
In this tutorial, we learned how to use telnet to send an email message through Gmail's SMTP server. Although this is not a practical way of sending email messages, it can be a helpful tool for troubleshooting SMTP-related issues.