我一直在尝试(并且失败)弄清楚如何通过 Python 发送电子邮件。
从这里尝试示例:http: //docs.python.org/library/smtplib.html#smtplib.SMTP
但server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
在我收到关于没有 SSL 连接的反弹后添加了这条线。
现在我得到了这个:
Traceback (most recent call last):
File "C:/Python26/08_emailconnects/12_29_EmailSendExample_NotWorkingYet.py", line 37, in <module>
server = smtplib.SMTP('smtp.gmail.com', 65)
File "C:\Python26\lib\smtplib.py", line 239, in __init__
(code, msg) = self.connect(host, port)
File "C:\Python26\lib\smtplib.py", line 295, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Python26\lib\smtplib.py", line 273, in _get_socket
return socket.create_connection((port, host), timeout)
File "C:\Python26\lib\socket.py", line 512, in create_connection
raise error, msg
error: [Errno 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
>>>
想法?
server = smtplib.SMTP("smtp.google.com", 495) 给我一个超时错误。只是 smtplib.smtp("smtp.google.com", 495) 给了我“SSLError: [Errno 1] _ssl.c:480: error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol”(见下文)。
我正在尝试不同的端口,现在我遇到了一个全新的错误。我只会发布整个代码,我可能会犯一些新手错误。
"
import smtplib
mailuser = 'MYEMAIL@gmail.com'
mailpasswd = 'MYPASSWORD'
fromaddr = 'MYEMAIL@gmail.com'
toaddrs = 'MYEMAIL2@gmail.com'
msg = 'Hooooorah!'
print msg
server = smtplib.SMTP_SSL('smtp.google.com')
server = smtplib.SMTP_SSL_PORT=587
server.user(mailuser)
server.pass_(mailpasswd)
server.set_debuglevel(1)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
"
然后我收到此错误消息:“
Traceback (most recent call last):
File "C:/Python26/08_emailconnects/12_29_eMAILSendtryin_stripped.py", line 16, in <module>
server = smtplib.SMTP_SSL('smtp.google.com')
File "C:\Python26\lib\smtplib.py", line 749, in __init__
SMTP.__init__(self, host, port, local_hostname, timeout)
File "C:\Python26\lib\smtplib.py", line 239, in __init__
(code, msg) = self.connect(host, port)
File "C:\Python26\lib\smtplib.py", line 295, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Python26\lib\smtplib.py", line 755, in _get_socket
self.sock = ssl.wrap_socket(self.sock, self.keyfile, self.certfile)
File "C:\Python26\lib\ssl.py", line 350, in wrap_socket
suppress_ragged_eofs=suppress_ragged_eofs)
File "C:\Python26\lib\ssl.py", line 118, in __init__
self.do_handshake()
File "C:\Python26\lib\ssl.py", line 293, in do_handshake
self._sslobj.do_handshake()
SSLError: [Errno 1] _ssl.c:480: error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol
"
请注意,实际上看起来像“server = smtplib.SMTPSSLPORT=587”的实际上是“server = smtplib.SMTP underscore SSL underscore PORT=587”,这里有一些格式化的事情。