简体   繁体   中英

Flask-Mail times out trying to connect to Gmail

I have very small flask app which send emails using flask-mail but when I run it nothing happen no errors no exceptions nothing!

this is the code

from flask import Flask
from flask_mail import Mail, Message

app = Flask(__name__)

DEBUG = True
MAIL_SERVER = 'smtp.gmail.com'
MAIL_PORT = 465
MAIL_USE_TLS = False
MAIL_USE_SSL = True
MAIL_DEBUG = True
MAIL_USERNAME = "myemail@gmail.com"
MAIL_PASSWORD = "mypassword"

app.config.from_object(__name__)

mail = Mail(app)

@app.route("/")
def send_mail():
    msg = Message("Hello",
                  sender="myemail@gmail.com",
                  recipients=["another_email@gmail.com"],
                  body="just testing",
                  )
    mail.send(msg)
    return "Message sent"
if __name__ == '__main__':
    app.run()

Update

after very long time i got this error TimeoutError: [Errno 110] Connection timed out

it looks like I solve my problem first go to this page to unlock CAPTCHA

https://accounts.google.com/DisplayUnlockCaptcha

then to this page to allow access from less secure apps https://www.google.com/settings/security/lesssecureapps

Got the same as Mustapha.

Found this: https://www.linode.com/docs/guides/running-a-mail-server/

Sending Email on Linode

In an effort to fight spam, Linode restricts outbound connections on ports 25, 465, and 587 on all Linodes for new accounts created after November 5th, 2019.

If you have a need to send mail from your Linode, we ask that you first configure (1) valid DNS A records and (2) rDNS records for any Linodes that you plan to use to send mail. Then, open a Support ticket from the Linode Manager – we'll ask you to provide the name of the Linode(s) that will be used for mailing.

Once you've completed those steps and provided that information, our Support team will be happy to review your request.

我尝试了 Ali Faki 建议的解决方案,但它对我不起作用,但是当我进一步尝试(检查问题出在哪里的简单代码)时,结果发现云服务提供商(在我的情况下为 Linode)阻止了邮件端口) 并且您必须设置一个域名并与他们联系以再次打开它。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM