Python-smtp需要身份验证

2024-05-09 18:22:09 发布

您现在位置:Python中文网/ 问答频道 /正文

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

>>> import smtplib
>>> from email.mime.text import MIMEText
>>> msg = MIMEText('Test body')
>>> me = 'support@mywebsite.com'
>>> to = 'myemail@gmail.com'
>>> 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', 'support@mywebsite.com')

Tags: to端口fromimport服务器comsupportmsg