使用Python连接到SMTP(SSL或TLS) [英] Connect to SMTP (SSL or TLS) using Python
问题描述
我试图连接到Gmail SMTP邮件服务器并按照给出的框架代码执行任务。只允许使用套接字
s(所以不能使用 smtplib
)。我需要:发送 HELO
命令, MAIL FROM
, RCPT TO
和 DATA
。
有许多类似的问题发布,但他们没有收到正确的答案。例如:
在Python中实现传输层安全性 - 简单的邮件客户端
该程序需要连接到 smtp.gmail.com
通过端口 587
。我采取了两种不同的方法:使用STARTTLS:
-
mailserver ='smtp.gmail.com'
clientSocket = socket(AF_INET,SOCK_STREAM)
clientSocket.connect((mailserver,587))
recv = clientSocket.recv(1024)
如果recv [:3]!='220'打印recv
:
打印'220未收到服务器回复'
#发送HELO命令和打印服务器响应
heloCommand ='HELO Alice \r\'
clientSocket.send(heloCommand)
recv1 = clientSocket.recv(1024)
打印recv1
如果recv1 [:3]!='250':
print'250 reply not received from server。'
#发送MAIL FROM命令和打印服务器响应。
command =STARTTLS\r\\\
clientSocket.send(command)
recvdiscard = clientSocket.recv(1024)
print recvdiscard
clientSocket.send (MAIL From:email \r\\\
)
recv2 = clientSocket.recv(1024)
打印recv2
如果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
220 2.0.0准备启动TLS
250未收到来自服务器的回复。
使用SSL时,我和链接的文章一样:
ssl.SSLError:[Errno 1] _ssl.c:504:error:140770FC:SSL例程:SSL23_GET_SERVER_HELLO:未知协议
我在这里错过了什么吗?我想我最好的选择是使用TLS,但我不知道如何去做...我的 MAIL FROM
命令有什么问题吗?
ssl.wrap_socket
,你稍后再做 - 特别是在收到 220
对 STARTTLS 命令。在完成 STARTTLS
之后,你应该再次执行 HELO
,因为服务器应该忘记之前发生的任何事情 STARTTLS
。
无论哪种情况,smtp.google.com端口465和587上的服务器仍然赢得对 MAIL
命令返回 250
响应,因为它们要求您在发送邮件之前进行身份验证。您将获得 530
响应。您需要使用 AUTH
命令和您的gmail.com凭证进行身份验证,然后才能成功使用 MAIL
这些服务器。
如果您不想进行身份验证,并根据您需要执行的操作的详细信息,可以尝试使用服务器的端口25 gmail.com的MX记录。目前,服务器是gmail-smtp-in.l.google.com并支持STARTTLS。
I am attempting to connect to the Gmail SMTP mail server and perform tasks as outlined by the skeleton code given to me. Only the use of socket
s is allowed (so not the smtplib
). I need to: send HELO
command, MAIL FROM
, RCPT TO
, and DATA
.
There are many cases of similar problems posted, but they haven't received the proper answer. For example:
Implementing Transport Layer Security in Python - Simple Mail Client
The program is required to connect to smtp.gmail.com
over port 587
. I've taken two different approaches:
Using 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.'
Using SSL:
clientSocketSSL = ssl.wrap_socket(clientSocket)
Then clientSocketSSL
replaces all instances of clientSocket
. The STARTTLS lines are also removed and import ssl
is added to the top.
When using the first method, the MAIL FROM:
command isn't returning anything. I'm getting the following output:
250 mx.google.com at your service
220 2.0.0 Ready to start TLS
250 reply not received from server.
When using SSL, I'm getting the same as the linked post:
ssl.SSLError: [Errno 1] _ssl.c:504: error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol
Am I missing something here? I guess my best option is to use TLS but I have no idea how to go about that... is there something wrong with my MAIL FROM
command?
解决方案 When using SSL, you need to connect to port 465 instead of port 587. If you use STARTTLS, you still need to use ssl.wrap_socket
, you just do it later - specifically, after receiving the 220
response to the STARTTLS
command. After doing STARTTLS
, you're supposed to do HELO
again, since the server is supposed to forget anything that happened before the STARTTLS
.
In either case, the servers at smtp.google.com ports 465 and 587 still won't return a 250
response to the MAIL
command, since they require that you are authenticated before you send mail. You'll get a 530
response instead. You'll need to use the AUTH
command with your gmail.com credentials to authenticate before you can use MAIL
successfully on those servers.
If you don't want to authenticate, and depending on the details of what you need to do, you could try using port 25 of the server found in gmail.com's MX record. At the moment, the server is gmail-smtp-in.l.google.com and supports STARTTLS.
这篇关于使用Python连接到SMTP(SSL或TLS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!