Python 3-向Gmail发送用户名后套接字SMTP超时

Python 3-向Gmail发送用户名后套接字SMTP超时,python,sockets,smtp,gmail,Python,Sockets,Smtp,Gmail,不确定以前是否有人问过这个问题,但我找不到任何似乎有效的解决方案 我在Windows10机器上用Python3.6.6运行这个 在学校作业中,我的任务是编写一个Python脚本,通过SMTP和套接字库连接到Gmail,并发送一条示例消息。下面是我的全部代码,因为我不确定是什么导致了它的问题: import socket, ssl, base64 # Initialize variables msg = "\r\n I love computer networks!" endmsg = "\r\

不确定以前是否有人问过这个问题,但我找不到任何似乎有效的解决方案

我在Windows10机器上用Python3.6.6运行这个

在学校作业中,我的任务是编写一个Python脚本,通过SMTP和套接字库连接到Gmail,并发送一条示例消息。下面是我的全部代码,因为我不确定是什么导致了它的问题:

import socket, ssl, base64

# Initialize variables
msg = "\r\n I love computer networks!"
endmsg = "\r\n.\r\n"
username = '***' # My email address went here
password = '***' # I put my password here

# Choose a mail server (e.g. Google mail server) and call it mailserver
mailserver = "smtp.gmail.com"
port = 465

# Send and get response
def send_com(in_string, response_num):

    # Only send content if provided, and if in string format, convert to bytes
    if in_string != '':
        if type(in_string) == str: in_string = in_string.encode()
        ssl_clientSocket.send(in_string)

    # Get response
    recv = ssl_clientSocket.recv(1024).decode()

    # If the first three numbers of the response from the server are not  '250', we have a problem
    if recv[:3] != response_num and response_num != '':
        print (recv[:3] + ' reply not received from server.')
    else:
        print (recv)

# Create socket called clientSocket and establish a TCP connection with mailserver
clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ssl_clientSocket = ssl.wrap_socket(clientSocket) 
ssl_clientSocket.connect((mailserver, port))

send_com('', '220')                                     # SMTP server initialization
send_com('HELO Alice\r\n', '250')                       # Send HELO command and print server response.
send_com('AUTH LOGIN\r\n', '334')                       # Authenticate mail server

print ("Username: " + base64.b64encode(username.encode()).decode())
print ("Password: " + base64.b64encode(password.encode()).decode() + "\n")

send_com(base64.b64encode(username.encode()), '334')    # Send username
send_com(base64.b64encode(password.encode()), '235')    # Sned password
send_com('MAIL From: <' + username + '>\r\n', '250')    # Send MAIL FROM command and print server response.
send_com('MAIL From: <' + username + '>\r\n', '250')    # Send RCPT TO command and print server response.
send_com('DATA\r\n', '250')                             # Send DATA command and print server response.

send_com(msg, '')       # Send message data.
send_com(endmsg, '')    # Message ends with a single period.

send_com('QUIT\r\n', '221') # Send QUIT command and get server response.

#recv5 = ssl_clientSocket.recv(I1024).decode()
导入套接字、ssl、base64
#初始化变量
msg=“\r\n我喜欢计算机网络!”
endmsg=“\r\n.\r\n”
用户名='***'#我的电子邮件地址在这里
password='***'#我把密码放在这里了
#选择一个邮件服务器(如谷歌邮件服务器)并称之为邮件服务器
mailserver=“smtp.gmail.com”
端口=465
#发送和获取响应
def send_com(字符串形式,响应编号):
#仅发送提供的内容,如果是字符串格式,则转换为字节
如果在字符串中!='':
如果类型(in_string)==str:in_string=in_string.encode()
ssl\u clientSocket.send(字符串形式)
#得到回应
recv=ssl\u clientSocket.recv(1024).decode()
#如果来自服务器的响应的前三个数字不是“250”,则我们有问题
如果接收[:3]!=响应数量和响应数量!='':
打印(recv[:3]+“未从服务器收到答复”。)
其他:
打印(recv)
#创建名为clientSocket的套接字,并与mailserver建立TCP连接
clientSocket=socket.socket(socket.AF\u INET,socket.SOCK\u流)
ssl\u clientSocket=ssl.wrap\u socket(clientSocket)
ssl_clientSocket.connect((邮件服务器,端口))
发送com(“220”)#SMTP服务器初始化
发送\u com('HELO Alice\r\n','250')#发送HELO命令并打印服务器响应。
发送\u com('AUTH LOGIN\r\n','334')#验证邮件服务器
打印(“用户名:+base64.b64encode(Username.encode()).decode())
打印(“密码:+base64.b64encode(Password.encode()).decode()+”\n”)
发送com(base64.b64encode(username.encode(),'334')#发送用户名
发送com(base64.b64encode(password.encode(),'235')#Sned密码
send_com('MAIL From:\r\n','250')#send MAIL From命令并打印服务器响应。
发送com(“邮件发件人:\r\n”,“250”)#将RCPT发送到命令并打印服务器响应。
send_com('DATA\r\n','250')#发送数据命令并打印服务器响应。
发送com(消息“”)#发送消息数据。
send_com(endmsg.)#消息以一个句点结束。
send_com('QUIT\r\n','221')#发送QUIT命令并获取服务器响应。
#recv5=ssl\u clientSocket.recv(I1024).decode()
我知道它在将我的编码用户名发送到谷歌服务器后会挂起。上面的所有代码似乎都会执行并返回适当的服务器响应,没有任何问题。但是,一旦到达发送编码用户名的命令,程序就会挂起,直到我关闭终端

send_com('AUTH LOGIN\r\n', '334')                       # Authenticate mail server

print ("Username: " + base64.b64encode(username.encode()).decode())
print ("Password: " + base64.b64encode(password.encode()).decode() + "\n")

send_com(base64.b64encode(username.encode()), '334')    # Send username
send_com(base64.b64encode(password.encode()), '235')    # Sned password
看起来您对登录身份验证方法的工作原理有错误的理解。基本上是这样的:

>  AUTH LOGIN
 < 334 base64("Username:")
>  base64(your_username)
 < 334 base64("Password:")
>  base64(your_password)
send_com('AUTH LOGIN\r\n', '334')                       # Authenticate mail server

send_com(base64.b64encode(username.encode()) + "\r\n", '334')    # Send username
send_com(base64.b64encode(password.encode()) + "\r\n", '235')    # Send password
您发送用户名和密码的两行已消失,但剩下的是您再次发送用户名和密码的其他行-仅在这些情况下,还会发送新行

这将使它不再被卡在原来的位置

它稍后会投诉,因为您将命令改为将
邮件从
发送到
MAIL FROM
,然后再将
RCPT发送到
。而且它会卡在
send\u com(msg,”)
上,因为您的
send\u com
函数会尝试从服务器读取内容,即使您不希望返回任何内容。代码还会抱怨服务器返回的某些状态代码与您期望的不一样。但一旦修复,邮件将成功发送

看起来您对登录身份验证方法的工作原理有错误的理解。基本上是这样的:

>  AUTH LOGIN
 < 334 base64("Username:")
>  base64(your_username)
 < 334 base64("Password:")
>  base64(your_password)
send_com('AUTH LOGIN\r\n', '334')                       # Authenticate mail server

send_com(base64.b64encode(username.encode()) + "\r\n", '334')    # Send username
send_com(base64.b64encode(password.encode()) + "\r\n", '235')    # Send password
您发送用户名和密码的两行已消失,但剩下的是您再次发送用户名和密码的其他行-仅在这些情况下,还会发送新行

这将使它不再被卡在原来的位置


它稍后会投诉,因为您将命令改为将
邮件从
发送到
MAIL FROM
,然后再将
RCPT发送到
。而且它会卡在
send\u com(msg,”)
上,因为您的
send\u com
函数会尝试从服务器读取内容,即使您不希望返回任何内容。代码还会抱怨服务器返回的某些状态代码与您期望的不一样。但是,一旦这个问题得到解决,邮件将成功发送。

为什么不使用
smtplib
?我会的,但我的教授明确告诉我不要。为什么不使用
smtplib
?我会的,但我的教授明确告诉我不要。谢谢你周到的回复!感谢您周到的回复!