4

我有一个任务是编写一个简单的邮件客户端,并使用套接字(不使用 smtp lib)连接到谷歌 smtp 服务器。但是,向 google smtp 服务器发出 MAIL FROM 命令需要 ssl 或 tls,这是我无法弄清楚的部分。因此,我正在尝试使用 Python 的 ssl.wrap_socket() 方法....

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

...这是行不通的。我很确定我需要包含 ca_certs 和 ca_reqs 参数,但我不确定。如果是这样,我该如何获得这些证书?我必须下载 openssl 并生成一个吗?有这方面经验的人吗?为了安全起见,这是整个代码。

from socket import *
import ssl

msg = "\r\n I love computer networks!"
endmsg = "\r\n.\r\n"

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

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

recv = ssl_clientSocket.recv(1024)
print
print recv

# If the first three numbers of what we receive from the SMTP server are not
# '220', we have a problem
if recv[:3] != '220':
    print '220 reply not received from server.'

# Send HELO command and print server response.
heloCommand = 'HELO Alice\r\n'
ssl_clientSocket.send(heloCommand)
recv1 = ssl_clientSocket.recv(1024)
print recv1

# If the first three numbers of the response from the server are not
# '250', we have a problem
if recv1[:3] != '250':
    print '250 reply not received from server.'

# Send MAIL FROM command and print server response.
mailFromCommand = 'MAIL From: wgimson@gmail.com\r\n'
ssl_clientSocket.send(mailFromCommand)
recv2 = ssl_clientSocket.recv(1024)
print recv2

# If the first three numbers of the response from the server are not
# '250', we have a problem
if recv2[:3] != '250':
    print '250 reply not received from server.'

# Send RCPT TO command and print server response.
rcptToCommand = 'RCPT To: macyali@gmail.com\r\n'
ssl_clientSocket.send(rcptToCommand)
recv3 = ssl_clientSocket.recv(1024)
print recv3

# If the first three numbers of the response from the server are not
# '250', we have a problem
if recv3[:3] != '250':
    print '250 reply not received from server.'

# Send DATA command and print server response.
dataCommand = 'DATA\r\n'
ssl_clientSocket.send(dataCommand)
recv4 = ssl_clientSocket.recv(1024)
print recv4

# If the first three numbers of the response from the server are not
# '250', we have a problem
if recv4[:3] != '250':
    print '250 reply not received from server.'

# Send message data.
ssl_clientSocket.send(msg)

# Message ends with a single period.
ssl_clientSocket.send(endmsg)

# Send QUIT command and get server response.
quitCommand = 'QUIT\r\n'
ssl_clientSocket.send(quitCommand)
recv5 = ssl_clientSocket.recv(I1024)
print recv5

# If the first three numbers of the response from the server are not
# '250', we have a problem
if recv5[:3] != '221':
    print '221 reply not received from server.'
4

2 回答 2

2

您正在连接到端口 587,这是 STARTTLS 的端口。使用 SMTPS 时(即在任何其他通信之前包装套接字),您需要连接到端口 465 而不是端口 587。如果您使用 STARTTLS,则在使用STARTTLS命令并收到220对它的响应之后,稍后包装套接字。做完之后STARTTLS,你应该再做HELO一次,因为服务器应该忘记之前发生的任何事情STARTTLS

无论哪种情况,smtp.google.com 端口 465 和 587 上的服务器仍然不会返回对该命令的250响应MAIL,因为它们要求您在发送邮件之前进行身份验证。相反,您会收到530回复。您需要使用AUTH带有 gmail.com 凭据的命令进行身份验证,然后才能MAIL在这些服务器上成功使用。

如果您不想进行身份验证,并且根据您需要做什么的详细信息,您可以尝试使用在 gmail.com 的 MX 记录中找到的服务器的端口 25。目前,服务器是 gmail-smtp-in.l.google.com 并支持 STARTTLS。

于 2012-10-21T23:54:29.093 回答
0
 recv5 = ssl_clientSocket.recv(I1024) 

I端口号中有一个,这是导致您的问题吗?或者你已经修复了那个部分?

于 2012-10-14T02:47:04.397 回答