L.T: 好的,现在它有效。我必须在配置后实例化Mail()类(即app.config):)
我有这个错误:
host = smtplib.SMTP(self.mail.server, self.mail.port)
File "C:\Python27\lib\smtplib.py", line 256, in __init__
(code, msg) = self.connect(host, port)
File "C:\Python27\lib\smtplib.py", line 316, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Python27\lib\smtplib.py", line 291, in _get_socket
return socket.create_connection((host, port), timeout)
File "C:\Python27\lib\socket.py", line 571, in create_connection
raise err
error: [Errno 10061] No connection could be made because the target machine actively refused it
我正在尝试为自己构建一个网站,由于上述错误,我因发送电子邮件而陷入困境。事情是我打开我的端口(那是因为我在路由器后面)。有一点需要注意的是,使用'standalone'smtplib可以正常工作,但烧瓶中没有。我会发布什么有用但没有。我不知道是什么问题。我将地址从socket.py修改为我的'localhost'和端口465,因为我认为这是问题所在。因此,我尝试了几乎所有我能做到的但是有用。有人可以帮我一点吗?感谢。
这不起作用(hello.py):
from flask import Flask
from flask_mail import Mail
from flask_mail import Message
from flask.ext.script import Manager
import os
mail = Mail()
app = Flask(__name__)
mail.init_app(app)
mail = Mail(app)
manager = Manager(app)
app.config['MAIL_SERVER'] = 'smtp.gmail.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USE_SSL'] = True
app.config['MAIL_USERNAME'] = os.environ.get('MAIL_USERNAME')
app.config['MAIL_PASSWORD'] = os.environ.get('MAIL_PASSWORD')
@app.route('/')
def index():
msg = Message('Hello',
sender = 'my_mail@gmail.com',
recipients = ['my_mail@gmail.com'])
msg.body = 'testing'
msg.html = '<b>testing</b>'
mail.send(msg)
app.debug = True
if __name__ == '__main__':
manager.run()
尝试了一种更简约的方式,看看它是否有效,并且它有效,我可以发送邮件给自己(mail.py):
import smtplib
from hello import app
gmail_user = app.config['MAIL_USERNAME']
gmail_pwd = app.config['MAIL_PASSWORD']
FROM = 'my_mail@gmail.com'
TO = ['my_mail@gmail.com']
message = 'test'
server_ssl = smtplib.SMTP_SSL("smtp.gmail.com", 465)
server_ssl.ehlo() # optional, called by login()
server_ssl.login(gmail_user, gmail_pwd)
# ssl server doesn't support or need tls, so don't call server_ssl.starttls()
server_ssl.sendmail(FROM, TO, message)
#server_ssl.quit()
server_ssl.close()
print 'successfully sent the mail'
...凹凸
答案 0 :(得分:0)
它与对象无关。函数的设计方式与返回值的方式相同,您可以按照将输入放入列表的方式分配该值。
您只需运行该函数,获取它返回的值并将其放入变量中。