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

2022-01-17 00:00:00 python sockets smtp gmail

问题描述

我正在尝试连接到 Gmail SMTP 邮件服务器,并按照提供给我的框架代码执行任务.只允许使用 sockets(所以不允许使用 smtplib).我需要:发送 HELO 命令、MAIL FROMRCPT TODATA.

贴了很多类似问题的案例,但都没有得到正确的答案.例如:在 Python 中实现传输层安全性 - 简单邮件客户端

该程序需要通过端口 587 连接到 smtp.gmail.com.我采取了两种不同的方法:

  1. 使用 STARTTLS:

    mailserver = 'smtp.gmail.com'clientSocket = 套接字(AF_INET,SOCK_STREAM)clientSocket.connect((邮件服务器, 587))recv = clientSocket.recv(1024)打印接收如果 recv[:3] != '220':print '220 未收到来自服务器的回复.'#发送 HELO 命令并打印服务器响应heloCommand = 'HELO 爱丽丝
    'clientSocket.send(heloCommand)recv1 = clientSocket.recv(1024)打印recv1如果 recv1[:3] != '250':print '250 未收到来自服务器的回复.'#Send MAIL FROM 命令并打印服务器响应.命令 = "STARTTLS
    "clientSocket.send(命令)recvdiscard = clientSocket.recv(1024)打印recvdiscardclientSocket.send("MAIL From: email
    ")recv2 = clientSocket.recv(1024)打印recv2如果 recv2[:3] != '250':print '250 未收到来自服务器的回复.'

  2. 使用 SSL:

    clientSocketSSL = ssl.wrap_socket(clientSocket)

    然后 clientSocketSSL 替换 clientSocket 的所有实例.STARTTLS 行也被删除,import ssl 被添加到顶部.

使用第一种方法时,MAIL FROM: 命令没有返回任何内容.我得到以下输出:

250 mx.google.com 为您服务220 2.0.0 准备启动 TLS250 未收到来自服务器的回复.

使用 SSL 时,我得到的结果与链接的帖子相同:

ssl.SSLError: [Errno 1] _ssl.c:504: error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol

我在这里遗漏了什么吗?我想我最好的选择是使用 TLS,但我不知道该怎么做……我的 MAIL FROM 命令有问题吗?

解决方案

使用SSL时,需要连接465端口,而不是587端口.如果使用STARTTLS,仍然需要使用ssl.wrap_socket,您稍后再做 - 具体来说,在收到对 STARTTLS 命令的 220 响应之后.在执行STARTTLS之后,你应该再次执行HELO,因为服务器应该忘记STARTTLS之前发生的任何事情.p>

在任何一种情况下,smtp.google.com 端口 465 和 587 上的服务器仍然不会向 MAIL 命令返回 250 响应,因为它们需要您在发送邮件之前已通过身份验证.相反,您将收到 530 响应.您需要使用 AUTH 命令和 gmail.com 凭据进行身份验证,然后才能在这些服务器上成功使用 MAIL.

如果您不想进行身份验证,并且根据您需要执行的详细信息,您可以尝试使用 gmail.com 的 MX 记录中的服务器的端口 25.目前,服务器为 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 sockets 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:

  1. 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
    '
    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
    "
    clientSocket.send(command)
    recvdiscard = clientSocket.recv(1024)
    print recvdiscard
    clientSocket.send("MAIL From: email
    ")
    recv2 = clientSocket.recv(1024)
    print recv2
    if recv2[:3] != '250':
        print '250 reply not received from server.'
    

  2. 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.

相关文章