使用sendgrid的smtp服务,在代码中配置Mail时,报错 550, Unauthenticated senders not allowed

smtplib.SMTPSenderRefused

smtplib.SMTPSenderRefused:(550,b’Unauthenticated发送者不允许’,‘82749316 @qq.com’)

下面是我的一些配置
app.config.update(
SECRET_KEY=os.getenv(‘SECRET_KEY’, ‘secret string’),
MAIL_SERVER=os.getenv(‘MAIL_SERVER’),
MAIL_PORT=587,
MAIL_USE_TLS=True,
MAIL_USERNAME=os.getenv(‘MAIL_USERNAME’),
MAIL_PASSWORD=os.getenv(os.getenv(‘SENDGRID_API_KEY’)),
MAIL_DEFAULT_SENDER=(‘82749316@qq.com’)
)
.env中的
MAIL_SERVER=smtp.sendgrid.net
MAIL_USERNAME=apikey
SENDGRID_API_KEY=SG.qBOoAngQQM20Pq4A…

使用smtp服务需要做一个发件人身份验证吗, 但是我又没有域名。。。

这一行不对:

MAIL_DEFAULT_SENDER=(‘82749316@qq.com’)

没记错的话,这里应该是一个两元素元组,而且邮件地址应该和 MAIL_USERNAME 一致,具体见 P181。试试改成:

MAIL_DEFAULT_SENDER=('Your Name', os.getenv('MAIL_USERNAME'))

是的,我之前是这么设置的,也提示同样的错误。有点懵,可能是网络的问题吗?我用的是校园网,没有固定IP。我又重新注册了一个账号,也是不行。但是用国内的比如QQ邮箱的SMTP服务就可以。

发下完整的错误输出看看?

这是网页上的报错。

```
# smtplib.SMTPSenderRefused

smtplib.SMTPSenderRefused: (550, b'Unauthenticated senders not allowed', '=?utf-8?q?ddwade?= <2083297662@qq.com>')

## Traceback  *(most recent call last)*

* #### File "c:\users\asus\flask\helloflask\.venv\lib\site-packages\flask\app.py", line  *2309* , in  `__call__`

return self.wsgi_app(environ, start_response)

* #### File "c:\users\asus\flask\helloflask\.venv\lib\site-packages\flask\app.py", line  *2295* , in  `wsgi_app`

response = self.handle_exception(e)

* #### File "c:\users\asus\flask\helloflask\.venv\lib\site-packages\flask\app.py", line  *1741* , in  `handle_exception`

reraise(exc_type, exc_value, tb)

* #### File "c:\users\asus\flask\helloflask\.venv\lib\site-packages\flask\_compat.py", line  *35* , in  `reraise`

![](http://localhost:5000/?__debugger__=yes&cmd=resource&f=console.png)raise value

* #### File "c:\users\asus\flask\helloflask\.venv\lib\site-packages\flask\app.py", line  *2292* , in  `wsgi_app`

response = self.full_dispatch_request()

* #### File "c:\users\asus\flask\helloflask\.venv\lib\site-packages\flask\app.py", line  *1815* , in  `full_dispatch_request`

rv = self.handle_user_exception(e)

* #### File "c:\users\asus\flask\helloflask\.venv\lib\site-packages\flask\app.py", line  *1718* , in  `handle_user_exception`

reraise(exc_type, exc_value, tb)

* #### File "c:\users\asus\flask\helloflask\.venv\lib\site-packages\flask\_compat.py", line  *35* , in  `reraise`

raise value

* #### File "c:\users\asus\flask\helloflask\.venv\lib\site-packages\flask\app.py", line  *1813* , in  `full_dispatch_request`

rv = self.dispatch_request()

* #### File "c:\users\asus\flask\helloflask\.venv\lib\site-packages\flask\app.py", line  *1799* , in  `dispatch_request`

return self.view_functions[rule.endpoint](**req.view_args)

* #### File "C:\Users\ASUS\flask\helloflask\demos\email\app.py", line  *97* , in  `index`

send_smtp_mail(subject, to, body)

* #### File "C:\Users\ASUS\flask\helloflask\demos\email\app.py", line  *39* , in  `send_smtp_mail`

mail.send(message)

* #### File "c:\users\asus\flask\helloflask\.venv\lib\site-packages\flask_mail.py", line  *492* , in  `send`

message.send(connection)

* #### File "c:\users\asus\flask\helloflask\.venv\lib\site-packages\flask_mail.py", line  *427* , in  `send`

connection.send(self)

* #### File "c:\users\asus\flask\helloflask\.venv\lib\site-packages\flask_mail.py", line  *192* , in  `send`

message.rcpt_options)

* #### File "d:\py3.7\Lib\smtplib.py", line  *867* , in  `sendmail`

raise SMTPSenderRefused(code, resp, from_addr)

> smtplib.SMTPSenderRefused: (550, b'Unauthenticated senders not allowed', '=?utf-8?q?ddwade?= <2083297662@qq.com>')

这应该是终端和sendgrid的交互信息
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [06/Jun/2019 14:22:41] "POST / HTTP/1.1" 200 -
127.0.0.1 - - [06/Jun/2019 14:22:41] "GET /static/favicon.ico HTTP/1.1" 200 -
send: 'ehlo [100.64.126.4]\r\n'
reply: b'250-smtp.sendgrid.net\r\n'
reply: b'250-8BITMIME\r\n'
reply: b'250-PIPELINING\r\n'
reply: b'250-SIZE 31457280\r\n'
reply: b'250-STARTTLS\r\n'
reply: b'250-AUTH PLAIN LOGIN\r\n'
reply: b'250 AUTH=PLAIN LOGIN\r\n'
reply: retcode (250); Msg: b'smtp.sendgrid.net\n8BITMIME\nPIPELINING\nSIZE 31457280\nSTARTTLS\nAUTH PLAIN LOGIN\nAUTH=PLAIN LOGIN'
send: 'STARTTLS\r\n'
reply: b'220 Begin TLS negotiation now\r\n'
reply: retcode (220); Msg: b'Begin TLS negotiation now'
send: 'ehlo [100.64.126.4]\r\n'
reply: b'250-smtp.sendgrid.net\r\n'
reply: b'250-8BITMIME\r\n'
reply: b'250-PIPELINING\r\n'
reply: b'250-SIZE 31457280\r\n'
reply: b'250-STARTTLS\r\n'
reply: b'250-AUTH PLAIN LOGIN\r\n'
reply: b'250 AUTH=PLAIN LOGIN\r\n'
reply: retcode (250); Msg: b'smtp.sendgrid.net\n8BITMIME\nPIPELINING\nSIZE 31457280\nSTARTTLS\nAUTH PLAIN LOGIN\nAUTH=PLAIN LOGIN'
send: 'mail FROM:<2083297662@qq.com> size=360\r\n'
reply: b'550 Unauthenticated senders not allowed\r\n'
reply: retcode (550); Msg: b'Unauthenticated senders not allowed'
send: 'rset\r\n'
reply: b'250 I remember nothing\r\n'
reply: retcode (250); Msg: b'I remember nothing'
send: 'quit\r\n'
reply: b'221 See you later\r\n'
reply: retcode (221); Msg: b'See you later'
127.0.0.1 - - [06/Jun/2019 14:22:55] "POST / HTTP/1.1" 500 -
```

你确定你的密钥是正确的吗?

是的,当时刚注册了一个账号,新建了密钥,复制黏贴了下来。 但是好像是因为VPN或是其他的问题,账号好像给禁掉了,变成了no authenticated。当天账号是能用的无疑。 上述的配置也都跟问题发布的时候是一样的。

这么关键的信息你到现在才说……账号都被封了,你怎么可能发出去邮件?

P.S. 对下次发帖的建议:

  • 不要把错误信息翻译成中文
  • 对报错、代码和各类输出使用代码块格式
  • 描述尽可能包含所有相关信息

不是呀- - ,我的意思是我提出问题的那天,账号刚创建是能用的,配置也试过几种了,包括上面您给出的配置方式。

这样的话,建议重新生成一个新的密钥试一下。发件人配置改成下面这样:

MAIL_DEFAULT_SENDER='Your Name <noreply@example.com>'