简体   繁体   English

使用Python连接到SMTP(SSL或TLS)

[英]Connect to SMTP (SSL or TLS) using Python

I am attempting to connect to the Gmail SMTP mail server and perform tasks as outlined by the skeleton code given to me. 我正在尝试连接到Gmail SMTP邮件服务器,并按照给我的框架代码所述执行任务。 Only the use of socket s is allowed (so not the smtplib ). 只允许使用socket s(因此不允许使用smtplib )。 I need to: send HELO command, MAIL FROM , RCPT TO , and DATA . 我需要:发送HELO命令, MAIL FROMRCPT TODATA

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 例如: 在Python中实现传输层安全性 - 简单邮件客户端

The program is required to connect to smtp.gmail.com over port 587 . 该程序需要通过端口587连接到smtp.gmail.com I've taken two different approaches: 我采取了两种不同的方法:

  1. Using STARTTLS: 使用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.' 
  2. Using SSL: 使用SSL:

     clientSocketSSL = ssl.wrap_socket(clientSocket) 

    Then clientSocketSSL replaces all instances of clientSocket . 然后clientSocketSSL替换的所有实例clientSocket The STARTTLS lines are also removed and import ssl is added to the top. STARTTLS行也被删除, import ssl被添加到顶部。

When using the first method, the MAIL FROM: command isn't returning anything. 使用第一种方法时, MAIL FROM:命令不返回任何内容。 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时,我与链接的帖子相同:

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? 我想我最好的选择是使用TLS,但我不知道如何去做...我的MAIL FROM命令有什么问题吗?

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. 使用SSL时,您需要连接到端口465而不是端口587.如果您使用STARTTLS,您仍然需要使用ssl.wrap_socket ,您只需稍后再执行 - 特别是在收到对STARTTLS命令的220响应之后。 After doing STARTTLS , you're supposed to do HELO again, since the server is supposed to forget anything that happened before the STARTTLS . 在做STARTTLS ,你应该再次进行HELO ,因为服务器应该忘记在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. 在任何一种情况下,smtp.google.com端口465和587上的服务器仍然不会返回对MAIL命令的250响应,因为它们要求您在发送邮件之前进行身份验证。 You'll get a 530 response instead. 你会收到530响应。 You'll need to use the AUTH command with your gmail.com credentials to authenticate before you can use MAIL successfully on those servers. 在成功在这些服务器上使用MAIL之前,您需要使用带有gmail.com凭据的AUTH命令进行身份验证。

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. 如果您不想进行身份验证,并且根据您需要执行的操作的详细信息,您可以尝试使用gmail.com MX记录中找到的服务器的端口25。 At the moment, the server is gmail-smtp-in.l.google.com and supports STARTTLS. 目前,服务器是gmail-smtp-in.l.google.com并支持STARTTLS。

STARTTLS之后,请致电

clientSocket = ssl.wrap_socket(clientSocket)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM