我一直在尝试(失败)弄清楚如何通过Python发送电子邮件。
从这里尝试示例:
http://docs.python.org/library/smtplib.html#smtplib.SMTP
但在收到有关没有SSL连接的退信后添加了server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
行。
现在我得到了:
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例程:SSL23_GET_SERVER_HELLO:未知协议(protocol)”(请参见下文)。
我正在尝试其他端口,但现在遇到了一个全新的错误。我将只发布全部代码,我可能正在犯一些菜鸟错误。
”
import smtplib
mailuser = '[email protected]'
mailpasswd = 'MYPASSWORD'
fromaddr = '[email protected]'
toaddrs = '[email protected]'
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
”
请注意,实际上看起来像“服务器= smtplib.SMTPSSLPORT = 587”的实际上是“服务器= smtplib.SMTP下划线SSL下划线PORT = 587”,这里正在进行某种格式化。
最佳答案
以下代码对我有用:
import smtplib
FROMADDR = "[email protected]"
LOGIN = FROMADDR
PASSWORD = "my.real.password"
TOADDRS = ["[email protected]"]
SUBJECT = "Test"
msg = ("From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n"
% (FROMADDR, ", ".join(TOADDRS), SUBJECT) )
msg += "some text\r\n"
server = smtplib.SMTP('smtp.gmail.com', 587)
server.set_debuglevel(1)
server.ehlo()
server.starttls()
server.login(LOGIN, PASSWORD)
server.sendmail(FROMADDR, TOADDRS, msg)
server.quit()
我正在使用Python 2.5.2。
编辑:按照ΤΖΩΤΙΙΟΥ的建议将端口从25更改为587,并删除第二个ehlo()。现在,我想知道为什么端口25在我的机器上可以正常运行(而端口465不能正常运行)。