连接重置错误: [WinError 10054] 远程主机 (SMTP) 强制关闭了现有连接



我正在尝试使用SMTP套接字将测试电子邮件从一个gmail帐户发送到另一个gmail帐户。我已允许从我的客户端 Gmail 帐户进行不安全的访问。

在登录阶段,代码在此处中断:

#Send username as Base64 encoded string
username = (username + 'rn').encode()
command = base64.b64encode(username)
clientSocket.send(command)
resp = clientSocket.recv(1024)
print('S: ' + resp.decode())

我收到 10054 错误消息,并尝试使用 wireshark 分解正在发生的事情,但我并不真正了解发生了什么。它似乎是基于谷歌smtp服务器的服务器端加密。我是套接字编程的新手,感谢您的耐心等待!

代码的其余部分:

from socket import *
import time
import base64
print("")
username = input("Enter your mail.com username : ")
password = input("Enter your mail.com password : ")
rec = input("nEnter the recipient's email address : ")
name = input("nEnter your name : ")
subject = input("nEnter the subject for your mail : n")
message = ["SMTP client is awesome rn", "123456789 rn","Networking is cool. rn"]
print("")
endmsg = "rn.rn"
# Choose a mail server (e.g. Google mail server) and call it mailserver
mailserver = "smtp.gmail.com"
mailserverPort = 587
# Create socket called clientSocket and establish a TCP connection with mailserver
clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect((mailserver, mailserverPort))
resp = clientSocket.recv(1024)
print('S: ' + resp.decode())
# Send EHLO (for Extended SMTP) command and print server response.
heloCommand = 'EHLO 127.0.0.1 rn'
print('C: ' + heloCommand)
clientSocket.send(heloCommand.encode())
resp = clientSocket.recv(1024)
print('S: ' + resp.decode())
# Authentication start mail server client
command = 'STARTTLS rn'
print('C: ' + command)
clientSocket.send(command.encode())
resp = clientSocket.recv(1024)
print('S: ' + resp.decode())
# Authentication required for the mail.com server I'm using
command = 'AUTH LOGIN rn'
print('C: ' + command)
clientSocket.send(command.encode())
resp = clientSocket.recv(1024)
print('S: ' + resp.decode())
# Send username as Base64 encoded string
username = (username + 'rn').encode()
command = base64.b64encode(username)
#print('C: ' + command.decode())
clientSocket.send(command)
resp = clientSocket.recv(1024)
print('S: ' + resp.decode())
# Send password as Base64 encoded string
password = (password + 'rn').encode()
command = base64.b64encode(password)
print('C: ' + command)
clientSocket.send(command)
resp = clientSocket.recv(1024)
print('S: ' + resp.decode())
# Send MAIL FROM command and print server response.
command = "MAIL FROM: <" + username + ">rn"
print('C: ' + command)
clientSocket.send(command)
resp = clientSocket.recv(1024)
print('S: ' + resp.decode())
# Send RCPT TO command and print(server resp.decode()onse.
command = "RCPT TO: <" + rec + ">rn"
print('C: ' + command)
clientSocket.send(command)
resp = clientSocket.recv(1024)
print('S: ' + resp.decode())
# Send DATA command and print server response.
command = "DATArn"
print('C: ' + command)
clientSocket.send(command)
resp = clientSocket.recv(1024)
print('S: ' + resp.decode())
# Send message headers
# Gmail does not accept mail without headers to prevent spam
command = 'From: ' + name + ' <' + username + '>' + 'rn'
print('C: ' + command)
clientSocket.send(command)
command = 'To: ' + rec + 'rn'
print('C: ' + command)
clientSocket.send(command)
command = 'Date: ' + time.asctime( time.localtime(time.time()) ) + 'rn'
print('C: ' + command)
clientSocket.send(command)
command = 'Subject: ' + subject + 'rn'
print('C: ' + command)
clientSocket.send(command)
# Send message data.
clientSocket.send('rn')
for msg in message:
print('C: ' + msg)
clientSocket.send(msg)
# Message ends with a single period.
print('C: ' + endmsg)
clientSocket.send(endmsg)
# Send QUIT command and get server resp.decode()onse.
command = "QUITrn"
print('C: ' + command)
clientSocket.send(command)
resp = clientSocket.recv(1024)
print('S: ' + resp.decode())

STARTTLS命令请求启动 SSL/TLS 加密会话。 但是您的代码中根本没有 SSL/TLS 逻辑。 如果服务器回复成功STARTTLS,则客户端必须启动并完成成功的 SSL/TLS 握手,然后才能发送任何进一步的 SMTP 命令。 这些命令(及其响应(需要由 SSL/TLS 会话加密。

由于您没有遵循协议,您正在被强制断开连接。 因此,要么不要使用STARTTLS(在这种情况下,您的AUTH可能会失败(,要么在您的套接字上正确实现 SSL/TLS。

顺便说一句,您不应该对您的Gmail帐户启用"不安全访问"。让 Gmail 创建一个应用专用密码,并使用该密码代替您的真实密码。

最新更新