当我运行以下内容时,我希望从我的Gmail帐户向我的Gmail帐户发送一封电子邮件。相反,我得到超时错误。我尝试过尝试过所有可能的组合,包括那些here
import os
from flask import Flask
from flask.ext.mail import Mail, Message
app =Flask(__name__)
mail=Mail(app)
USERNAME = 'my_gmail_username@gmail.com'
PASSWORD = 'my_gmail_password@gmail.com'
app.config.update(
MAIL_SERVER = 'smtp.gmail.com',
MAIL_PORT = 465,
MAIL_USE_SSL = True,
# MAIL_USE_TSL = True,
MAIL_USERNAME = USERNAME,
MAIL_PASSWORD = PASSWORD,
MAIL_FAIL_SILENTLY=False,
DEBUG = True)
mail=Mail(app)
@app.route("/")
def index():
msg = Message("Hello",
sender=USERNAME,
recipients=[USERNAME])
msg.body = "This is the email body"
mail.send(msg)
return "Sent"
if __name__ == "__main__":
app.run()
我明白了:
* Running on http://127.0.0.1:5000/
* Restarting with reloader
127.0.0.1 - - [03/Aug/2013 04:03:49] "GET / HTTP/1.1" 500 -
Traceback (most recent call last):
File "/home/drew/.virtualenvs/mail2/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/home/drew/.virtualenvs/mail2/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/home/drew/.virtualenvs/mail2/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/home/drew/.virtualenvs/mail2/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/home/drew/.virtualenvs/mail2/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/drew/.virtualenvs/mail2/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/drew/.virtualenvs/mail2/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/home/drew/.virtualenvs/mail2/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/drew/mail/postoffice.py", line 35, in index
mail.send(msg)
File "/home/drew/.virtualenvs/mail2/lib/python2.7/site-packages/flask_mail.py", line 415, in send
with self.connect() as connection:
File "/home/drew/.virtualenvs/mail2/lib/python2.7/site-packages/flask_mail.py", line 123, in __enter__
self.host = self.configure_host()
File "/home/drew/.virtualenvs/mail2/lib/python2.7/site-packages/flask_mail.py", line 135, in configure_host
host = smtplib.SMTP_SSL(self.mail.server, self.mail.port)
File "/usr/lib/python2.7/smtplib.py", line 776, in __init__
SMTP.__init__(self, host, port, local_hostname, timeout)
File "/usr/lib/python2.7/smtplib.py", line 265, in __init__
addr = socket.gethostbyname(socket.gethostname())
error: [Errno 110] Connection timed out
127.0.0.1 - - [03/Aug/2013 04:03:49] "GET /?__debugger__=yes&cmd=resource&f=style.css HTTP/1.1" 200 -
127.0.0.1 - - [03/Aug/2013 04:03:49] "GET /?__debugger__=yes&cmd=resource&f=jquery.js HTTP/1.1" 200 -
127.0.0.1 - - [03/Aug/2013 04:03:49] "GET /?__debugger__=yes&cmd=resource&f=debugger.js HTTP/1.1" 200 -
127.0.0.1 - - [03/Aug/2013 04:03:49] "GET /?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 200 -
127.0.0.1 - - [03/Aug/2013 04:03:49] "GET /?__debugger__=yes&cmd=resource&f=source.png HTTP/1.1" 200
答案 0 :(得分:4)
堆栈跟踪表示在socket.gethostbyname调用中发生超时。在执行DNS查找后,此函数将获取主机名(可能是您设置中的"smtp.gmail.com"
)并将其转换为IP地址。
该功能超时的事实表明运行此功能的系统无法解析名称smtp.gmail.com
。
此失败的最明显原因是系统未连接到网络,或网络设置阻止其连接。
您可以尝试从命令行ping主机名,看看会发生什么:
$ ping smtp.gmail.com
Pinging gmail-smtp-msa.l.google.com [74.125.25.108] with 32 bytes of data:
Reply from 74.125.25.108: bytes=32 time=17ms TTL=47
Reply from 74.125.25.108: bytes=32 time=16ms TTL=47
Reply from 74.125.25.108: bytes=32 time=18ms TTL=47
Reply from 74.125.25.108: bytes=32 time=17ms TTL=47
Ping statistics for 74.125.25.108:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 16ms, Maximum = 18ms, Average = 17ms