【发布时间】:2016-04-09 11:02:05
【问题描述】:
出于学习目的,我只想使用套接字和 SSL 库,因此我设法连接到 google.com 并登录到我的帐户。问题是我没有设法发送任何电子邮件,我无法理解原因。
这是输出,所以直到DATA 一切都是正确的。因此,我认为问题必须在.send("DATA"....) 之后的行中。
我使用的是python 3,因此我每次都需要.encode()和.decode()。
import ssl
import base64
from socket import *
import sys
cc = socket(AF_INET, SOCK_STREAM)
cc.connect(("smtp.gmail.com", 587))
print(cc.recv(1024).decode())
cc.send(('helo tester.com\r\n').encode())
print(cc.recv(1024).decode())
cc.send(('starttls\r\n').encode())
print(cc.recv(1024).decode())
############# Authentication #############
scc = ssl.wrap_socket(cc, ssl_version=ssl.PROTOCOL_SSLv23)
scc.send(('auth login\r\n').encode())
print(scc.recv(1024).decode())
scc.send((base64.b64encode(('francesco.zuppichini@gmail.com').encode()))+('\r\n').encode())
print(scc.recv(1024).decode())
scc.send((base64.b64encode(('********').encode()))+('\r\n').encode())
print(scc.recv(1024).decode())
############# EMAIL #############
scc.send(("MAIL FROM: <francesco.zuppichini@gmail.com>" + '\r\n').encode())
print(scc.recv(1024).decode())
scc.send(("RCPT to: <francesco.zuppichini@gmail.com>" + '\r\n').encode())
print(scc.recv(1024).decode())
scc.send(("DATA" + '\r\n').encode())
print(scc.recv(1024).decode())
# start to send the Data
scc.send(("Subject: Test!" + '\r\n').encode())
scc.send(("From: francesco.zuppichini@gmail.com" + '\r\n').encode())
scc.send(("To: francesco.zuppichini@gmail.com" + '\r\n').encode())
scc.send(("Ciaooone" + '\r\n').encode())
scc.send(("'\r\n'.'\r\n'").encode())
print(scc.recv(1024).decode())
############# Exit #############
scc.send(("QUIT" + '\r\n').encode())
print(scc.recv(1024).decode())
scc.close()
cc.close()
输出:
220 smtp.gmail.com ESMTP bg10sm91176924wjb.46 - gsmtp
250 smtp.gmail.com at your service
220 2.0.0 Ready to start TLS
334 VXNlcm5hbWU6
334 UGFzc3dvcmQ6
235 2.7.0 Accepted
250 2.1.0 OK bg10sm91176924wjb.46 - gsmtp
250 2.1.5 OK bg10sm91176924wjb.46 - gsmtp
354 Go ahead bg10sm91176924wjb.46 - gsmtp
【问题讨论】: