初始化代码:
app = create_app() # mail client mail = Mail(app)
配置文件:
# flask_mail MAIL_DEBUG = False MAIL_SUPPRESS_SEND = False MAIL_SERVER = 'smtp.qq.com' MAIL_PORT = 465 MAIL_USE_SSL = True MAIL_USE_TLS = False MAIL_USERNAME = os.getenv("MAIL_USERNAME") or "[email protected]" MAIL_PASSWORD = os.getenv("MAIL_PASSWORD") or "xxxxxxxxxxx" # QQ 邮箱授权码
发送邮件代码:
@celery.task def send(mail_from, mail_to, subject="", cOntent="", html=""): try: msg = Message( subject=subject, sender=mail_from, recipients=[mail_to] ) msg.body = content msg.html = html mail.send(msg) except Exception as e: return e.__str__() return "ok" send.delay("[email protected]", "[email protected]", "test", "content")
错误信息:
(535, b'Login Fail. Please enter your authorization code to login. More information in http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256')
不知道哪里出了问题,到网上搜了一圈,这个配置没错,就是账户验证失败,就很难受。
1 elboble 2022-10-10 17:15:11 +08:00 没仔细看,一般这种登录不是用户名密码的,而是生成一个 key ,返回有链接可以看下。 |
2 Courstick 2022-10-10 17:22:08 +08:00 @elboble 注释是写的 QQ 邮箱授权码 smtp = smtplib.SMTP_SSL('SMTP_SERVER', 465) smtp.login(username, passwd) smtp.sendmail(username, receiver, msg) smtp.quit() 这样试试呢 |
![]() | 3 kaiger OP @Courstick #2 smtplib.SMTPAuthenticationError: (535, b'Login Fail. Please enter your authorization code to login. More information in http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256') 还是报错,我之前把 QQ 的数字邮箱关掉了,不知道和这个有没有关系。 |
![]() | 5 kaiger OP @Courstick #4 ![]() 这个,SMTP 服务开了,还是发送不了 ``` def send(mail_from, mail_to, subject="", cOntent="", html=""): smtp = smtplib.SMTP_SSL('smtp.qq.com', 465) smtp.login("[email protected]", "tlkcxpzurpoxdjhe") smtp.sendmail(mail_from, mail_to, "hhh") smtp.quit() return "ok" send("[email protected]", "[email protected]") ``` |
![]() | 6 kaiger OP 大意了 |
![]() | 7 westoy 2022-10-10 17:39:28 +08:00 用 SES 啊, 几个平台一天都有几十封到百来封的免费额度的, 你这种程序发送业务其实用消费级邮件不太好, 他们提供 SMTP 也只是为了让你接入本地邮件软件的, 卡的很死的 |
8 Wongz 2022-10-10 19:35:16 +08:00 测试了一下 Flask-Mail ,QQ 邮箱可以正常发送,没有问题,也是使用了英文域名。看看是不是授权码弄错了吧,重新生成一个试试。 |
9 Wongz 2022-10-10 19:39:06 +08:00 配置只用了这些 ```python app.config['MAIL_SERVER']='smtp.qq.com' app.config['MAIL_PORT'] = 465 app.config['MAIL_USERNAME'] = '邮箱地址' app.config['MAIL_PASSWORD'] = '16 位授权码' app.config['MAIL_USE_TLS'] = False app.config['MAIL_USE_SSL'] = True ``` |
10 Wongz 2022-10-10 19:43:15 +08:00 按腾讯的说明,如果近期改了 QQ 密码,之前的授权码就会失效,要重新生成,可以看看是不是这个原因导致的。我也关闭了数字账号,测试可以正常发送 |
11 lonelinsky 2022-10-11 10:23:00 +08:00 生成号授权码之后先用一个邮箱客户端测试保证是好的,然后再在代码里面测试看看。(话说,腾讯的授权码不会显示登录的客户端个数吧?) |
12 julyclyde 2022-10-11 13:18:49 +08:00 @lonelinsky SMTP 客户端“没有自我”,无法互相区分的 |
14 elboble 2022-10-11 15:14:37 +08:00 这个可以用 ``` import smtplib _user = "号码 @qq.com" _pwd = "那啥 key" _to = "" def XEmail(fromAdd, pwd, toAdd ,subject ,attachfile ,msg): try: s = smtplib.SMTP_SSL("smtp.qq.com") s.login(fromAdd,pwd) s.sendmail(fromAdd,toAdd,msg.as_string()) s.quit() print "Done!" except smtplib.SMTPException,e: print "Fail at %s"%e ``` |
15 zhuzhipeng 2022-10-17 17:42:42 +08:00 魔狗在女孩的腿上是七八糟的,直到女孩拉它,狗很愚蠢: https://anymud.com |