我一直在尝试(并且失败)弄清楚如何通过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, inserver = 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:错误:140770FC:SSL例程:SSL23_GET_SERVER_HELLO:未知协议"(见下文).
我正在尝试不同的端口,现在我遇到了一个全新的错误.我只是发布了一些代码,我可能会犯一些菜鸟错误.
"
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, inserver = 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 下划线 SSL 下划线 PORT = 587",这里有某种格式化的事情.
以下代码适用于我:
import smtplib FROMADDR = "my.real.address@gmail.com" LOGIN = FROMADDR PASSWORD = "my.real.password" TOADDRS = ["my.real.address@gmail.com"] 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 没有).
使用SSL连接到GMail的正确方法是:
server = smtplib.SMTP('smtp.gmail.com', 587)
端口465似乎导致延迟.两个端口都在GMail FAQ中指定.
请注意,使用端口587对于SMTP over SSL更常见,尽管这只是一些简单的信息,但它没有其他实际用途.
这个答案作为社区维基提供,以便被选为"答案".请根据需要改进.
这是一个简单的扔掉解决方案.我想早点粘贴,但在我的椅子上睡着了.
import smtplib import email import os username = "user@gmail.com" passwd = "password" def mail(to, subject, text, attach): msg = MIMEMultipart() msg['From'] = username msg['To'] = to msg['Subject'] = subject msg.attach(MIMEText(text)) part = MIMEBase('application', 'octet-stream') part.set_payload(open(attach, 'rb').read()) Encoders.encode_base64(part) part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(attach)) msg.attach(part) server = smtplib.SMTP("smtp.gmail.com", 495) server.ehlo() server.starttls() server.ehlo() server.login(username, passwd) server.sendmail(username, to, msg.as_string()) server.close() mail("you", "hi", "hi", "webcam.jpg")
这是我的假设,这个线程上的大多数成功尝试使用其代码的人都不在win32上.;)
*编辑:有关一些好的"官方"示例,请参阅http://docs.python.org/library/email-examples.html.
问题是由于Python中的错误.尝试使用SMTP_SSL创建连接将失败,并显示"SMTPServerDisconnected:请先运行connect()".
已提交修复程序,因此您可以修补本地副本.请参阅名为"smtplib_72551.diff"的附件.
(注意:SMTP_SSL是添加到Python 2.6/3.0及更高版本的新类.)