从 Flask-Mail 发送邮件(SMTPSenderRefused 530)

Sending a mail from Flask-Mail (SMTPSenderRefused 530)

Flask Mail 应用程序中使用的应用程序配置(遵循 Miguel Grinberg Flask developlemt 书):

app.config['MAIL_SERVER'] = 'smtp.googlemail.com'
app.config['MAIL_PORT'] = 587
app.config['MAIL_USE_TLS'] = True
app.config['MAIL_USERNAME'] = os.environ.get('MAIL_USERNAME')
app.config['MAIL_PASSWORD'] = os.environ.get('MAIL_PASSWORD')

邮件用户名和密码变量已正确设置并重新检查。尝试使用以下代码发送消息时,

from flask.ext.mail import Message
from hello import mail
msg = Message('test subject', sender='same as MAIL_USERNAME', recipients=['check@mail.com'])
msg.body = 'text body'
msg.html = '<b>HTML</b> body'
with app.app_context():
    mail.send(msg)

发送时,应用程序一次又一次导致以下错误:

SMTPSenderRefused: (530, '5.5.1 Authentication Required. Learn more at\n5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 qb10sm6828974pbb.9 - gsmtp', u'configured MAIL_USERNAME')

该错误有任何解决方法吗?

在深入研究所面临的问题时,我重新检查了 Google、

的 SMTP 设置

改变

app.config['MAIL_SERVER'] = 'smtp.googlemail.com'

app.config['MAIL_SERVER'] = 'smtp.gmail.com'

成功了。

还要确保完整用户名用作Gmail SMTP用户名,即example@gmail.com 如上图所示。

希望对您有所帮助!!!

我也跟着这本书遇到了同样的问题,经过一番挖掘,我找到了问题的根本原因。但是,我不确定您的情况是否相同。

app.config['MAIL_USERNAME'] = os.environ.get('MAIL_USERNAME')
app.config['MAIL_PASSWORD'] = os.environ.get('MAIL_PASSWORD')

如您所见,您的 Flask 应用程序通过 os.environ.get() 获取您的电子邮件凭据,如果您在系统中临时设置此环境变量,在我的case Mac OSX,在你的终端会话之后它们将消失,所以你需要在下次进入终端时重新设置它们,如下所示:

export MAIL_USERNAME=**YOUR EMAIL**
export PASSWORD=**YOUR PASSWORD**

由于这种情况,我收到了这个错误,为了永久设置它们,您需要将这些变量包含在 .bash_profile[= 中21=] 文件在您的主目录中。

做这两件事来解决:

  1. 使用此 link 并打开 'Allow less secure apps'- https://myaccount.google.com/lesssecureapps

  2. 对电子邮件和密码使用硬编码值,效果很好。 只需在文件“init.py”中,编辑以下部分:

不要使用 os.environ.get

app.config['MAIL_USERNAME'] = 'youremail@gmail.com'
app.config['MAIL_PASSWORD'] = 'yourpassword'

您需要更改 Google 帐户设置。在 this page 上,打开 "Allow less secure apps" 的选项。

正如该页面所说:

Some apps and devices use less secure sign-in technology, which makes your account more vulnerable. You can turn off access for these apps, which we recommend, or turn on access if you want to use them despite the risks. Learn more