我正在尝试连接到 Gmail SMTP 邮件服务器并执行给我的框架代码所述的任务。只允许使用
有很多类似的问题,但它们没有得到正确的答案。例如:Implementing Transport Layer Security in Python - Simple Mail Client 该程序需要通过端口
socket
(而不是smtplib
)。我需要发送 HELO
命令、MAIL FROM
、RCPT TO
和 DATA
。有很多类似的问题,但它们没有得到正确的答案。例如:Implementing Transport Layer Security in Python - Simple Mail Client 该程序需要通过端口
587
连接到smtp.gmail.com
。我采用了两种不同的方法:
使用STARTTLS:
mailserver = 'smtp.gmail.com' clientSocket = socket(AF_INET, SOCK_STREAM) clientSocket.connect((mailserver, 587)) recv = clientSocket.recv(1024) print recv if recv[:3] != '220': print '220 reply not received from server.' #Send HELO command and print server response heloCommand = 'HELO Alice\r\n' clientSocket.send(heloCommand) recv1 = clientSocket.recv(1024) print recv1 if recv1[:3] != '250': print '250 reply not received from server.' #Send MAIL FROM command and print server response. command = "STARTTLS\r\n" clientSocket.send(command) recvdiscard = clientSocket.recv(1024) print recvdiscard clientSocket.send("MAIL From: email\r\n") recv2 = clientSocket.recv(1024) print recv2 if recv2[:3] != '250': print '250 reply not received from server.'
使用SSL:
clientSocketSSL = ssl.wrap_socket(clientSocket)
然后
clientSocketSSL
替换了所有的clientSocket
实例。 STARTTLS行也被删除,并在顶部添加了import ssl
。
使用第一种方法时,MAIL FROM:
命令没有返回任何内容。我得到了以下输出:
250 mx.google.com at your service
220 2.0.0 Ready to start TLS
250 reply not received from server.
使用SSL时,我得到了与链接的帖子相同的结果:
ssl.SSLError: [Errno 1] _ssl.c:504: error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol
这里我是否漏掉了什么?我猜我的最佳选项是使用TLS,但我不知道如何去做...我的MAIL FROM
命令有什么问题吗?