Python - smtp 需要身份验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14196581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 10:44:33  来源:igfitidea点击:

Python - smtp requires authentication

pythonemailauthenticationsmtpsmtplib

提问by Paris

I am trying to send an email using python but despite I am using the local SMTP server it seems that it needs authentication. The code I run and the error I get can be seen below. I use port 587, because port 25 cannot be opened on my server. Could you please help me on setting up the local SMTP server using python on port 587?

我正在尝试使用 python 发送电子邮件,但尽管我使用的是本地 SMTP 服务器,但它似乎需要身份验证。我运行的代码和我得到的错误可以在下面看到。我使用端口 587,因为在我的服务器上无法打开端口 25。你能帮我在端口 587 上使用 python 设置本地 SMTP 服务器吗?

>>> import smtplib
>>> from email.mime.text import MIMEText
>>> msg = MIMEText('Test body')
>>> me = '[email protected]'
>>> to = '[email protected]'
>>> msg['Subject'] = 'My Subject'
>>> msg['From'] = me
>>> msg['To'] = to
>>> s = smtplib.SMTP('localhost', 587)
>>> s.sendmail(me, [to], msg.as_string())

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/smtplib.py", line 722, in sendmail
    raise SMTPSenderRefused(code, resp, from_addr)
smtplib.SMTPSenderRefused: (530, '5.7.0 Authentication required', '[email protected]')

采纳答案by Jon Clements

Then before you attempt to use s.sendmail, you use s.login('user', 'password')- http://docs.python.org/2/library/smtplib.html#smtplib.SMTP.login

然后在您尝试使用之前s.sendmail,您使用s.login('user', 'password')- http://docs.python.org/2/library/smtplib.html#smtplib.SMTP.login

If you don't have login details - then consult your system admin.

如果您没有登录详细信息,请咨询您的系统管理员。