我的任务是编写一个简单的邮件客户端,并使用套接字(不使用smtp lib)连接到Google 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: [email protected]\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: [email protected]\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.'

最佳答案

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

无论哪种情况,位于smtp.google.com端口465和587的服务器仍然不会对250命令返回MAIL响应,因为它们要求您先经过身份验证才能发送邮件。您将得到一个530响应。您需要在gmail.com凭据上使用AUTH命令进行身份验证,然后才能在这些服务器上成功使用MAIL

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

10-08 12:34