与 smtp.gmail.com 的 TLS 连接无效

TLS connection to smtp.gmail.com isn't working

我试图弄清楚为什么与 smtp.gmail.com 的 TLS 连接不起作用,代码包括 import ssl 并且我在 STARTTLS 命令之前和之后执行了 HELO,然后我包装了ssl_wrap 的 clientSocket。我正在输入我的电子邮件地址和密码以进行授权。需要注意的一件事是我无法使用 smtplib。有谁知道我做错了什么?我可以继续检查错误编号是否可能有误,但它似乎在授权时崩溃了。

这是控制台中的输出,在我按回车键的最后一行之后不断出现空格:

代码如下:

from socket import * 
import ssl
import base64

msg = "\r\n I love computer networks!" 
endmsg = "\r\n.\r\n" 

#----------------------------------------------------------------------
# Choose a mail server (e.g. Google mail server) and call it mailserver 
#----------------------------------------------------------------------
mailserver = 'smtp.gmail.com' 
#mailServer = 'localhost'
mailPort = 587 #25 is standard smtp, 465 is for SSL and 587 for TLS

#---------------------------------------------------------------------------------
# Create socket called clientSocket and establish a TCP connection with mailserver 
#---------------------------------------------------------------------------------
clientSocket = socket(AF_INET, SOCK_STREAM) 
clientSocket.connect((mailserver, mailPort))
recv1 = clientSocket.recv(1024).decode() 
print(recv1) 
if recv1[:3] != '220': #250, 530
    print('220 reply not received from server.')
    
#---------------------------------------------
# Send HELO command and print server response. 
#---------------------------------------------
heloCommand = 'HELO Shane\r\n' 
clientSocket.sendall(heloCommand.encode()) 
recv3 = clientSocket.recv(1024).decode() 
print(recv3) 
if recv3[:3] != '530':  #250
    print('530 reply not received from server.')

#--------------------------------
# Request an encrypted connection
#--------------------------------
command = 'STARTTLS\r\n'.encode()
clientSocket.send(command)
recv2 = clientSocket.recv(1024).decode()
print(recv2)
if recv2[:3] != '220':
    print('recv2:220 reply not received from server')

#-------------------
# Encrypt the socket
#-------------------
ssl_clientSocket = ssl.wrap_socket(clientSocket)

#---------------------------------------------
# Send HELO command and print server response. 
#---------------------------------------------
heloCommand = 'HELO Shane\r\n' 
ssl_clientSocket.sendall(heloCommand.encode()) 
recv3 = ssl_clientSocket.recv(1024).decode() 
#print(recv3) 
if recv3[:3] != '530':  #250
    print('530 reply not received from server.')

#---------------------------------------------
# Send the AUTH LOGIN command and print server response.
#---------------------------------------------
authCommand = 'AUTH LOGIN\r\n'
ssl_clientSocket.write(authCommand.encode())
auth_recv = ssl_clientSocket.read(1024)
if auth_recv[:3] != '334':
    print ('334 reply not received from server')

#---------------------------------------------
# Send username and print server response.
#---------------------------------------------
username = input("Type your username and press enter:")
uname = base64.b64encode(username.encode())
print('\r\n')
ssl_clientSocket.write(uname)
uname_recv = ssl_clientSocket.read(1024)
if uname_recv[:3] != '334':
    print ('334 reply not received from server')

#---------------------------------------------
# Send password and print server response.
#---------------------------------------------
password = input("Type your password and press enter:")
pword = base64.b64encode(password.encode())
#print('\r\n')
ssl_clientSocket.write(pword)
pword_recv = ssl_clientSocket.read(1024)
if pword_recv[:3] != '235':
    print ('235 reply not received from server')

#--------------------------------------------------
# Send MAIL FROM command and print server response. 
#--------------------------------------------------
mailfromCommand = 'MAIL FROM: <toddhoward@bethesda.org>\r\n'
ssl_clientSocket.sendall(mailfromCommand.encode())
recv4 = ssl_clientSocket.recv(1024)
print(recv4)
if recv4[:3] != '530': #250, 530
    print('530 reply not received from server.')


#-------------------------------------------------
# Send RCPT TO command and print server response. 
#-------------------------------------------------
rcpttoCommand = 'RCPT TO: <whimay@iu.edu>\r\n'
ssl_clientSocket.sendall(rcpttoCommand.encode())
recv5 = ssl_clientSocket.recv(1024)
print(recv5)
if recv5[:3] != '530': #250
    print('rcpt to 530 reply not received from server.')
 
#---------------------------------------------
# Send DATA command and print server response.
#--------------------------------------------- 
dataCommand = 'Data\r\n'
print(dataCommand)
ssl_clientSocket.sendall(dataCommand.encode())
recv6 = ssl_clientSocket.recv(1024)
print(recv6)
if recv6[:3] != '530':  #250? 354, 530
    print('data 530 reply not received from server.')

#---------------------------------------------
# Send message data. 
#---------------------------------------------
message = 'SUBJECT: Obsidian made my best Fallout game'
fallout76 = 'SUBJECT: I’m sorry about Fallout 76\r\n'
ssl_clientSocket.send(fallout76.encode())
ssl_clientSocket.send(message.encode())
ssl_clientSocket.send(msg.encode())

#---------------------------------------------
# Message ends with a single period.
#--------------------------------------------- 
ssl_clientSocket.sendall(message.encode() + endmsg.encode())
recv7 = ssl_clientSocket.recv(1024)
print(recv7)
if recv7[:3] != '530': #250, 530
    print('end msg 530 reply not received from server.')

#---------------------------------------------
# Send QUIT command and get server response. 
#---------------------------------------------
quitCommand = 'Quit\r\n'
print(quitCommand)
ssl_clientSocket.sendall(quitCommand.encode())
recv8 = ssl_clientSocket.recv(1024)
print(recv8)
if recv8[:3] != '221': #250. 221, 530
    print('quit 221 reply not received from server.')

TLS connection to smtp.gmail.com isn't working

与此声明相反,TLS 正在运行。 TLS 升级成功,否则根本无法进一步通信。但 HELO 在 TLS 升级后按预期工作并产生“250 smtp.gmail.com at your service”。

登录过程出了问题。它无法在 base64 编码的用户名和密码后发送 b"\r\n"。这会导致连接挂起,因为服务器在发回响应之前需要更多数据。