如何使用smtplib实现python自动邮件发送器?

问题描述 投票:1回答:1

我正在尝试使用auto-mailer编写python smtplib。我已经在运行它,但是我一直在打我的except语句。

代码看起来像这样。

    sender = ['[email protected]']
    receivers = ['[email protected]']

    subject = "does this work?"
    message = "this is a cool email"

    try:
        server = smtplib.SMTP('smtp.gmail.com:587')
        server.ehlo()
        server.starttls()
        server.sendmail(sender, receivers, message)
        print("Successfully sent email")
        server.quit()
    except smtplib.SMTPException:
        print("Error: unable to send email")

感谢您的任何帮助。

编辑:堆栈跟踪如下

Traceback (most recent call last):
  File "C:/Users/......py", line 38, in <module>
    email_protocol()
  File "C:/Users/....py", line 33, in email_protocol
    server.sendmail(sender, receivers, message)
  File "C:\Users\...\lib\smtplib.py", line 867, in sendmail
    raise SMTPSenderRefused(code, resp, from_addr)
smtplib.SMTPSenderRefused: (530, b'5.7.0 Authentication Required. Learn more at\n5.7.0  https://support.google.com/mail/?p=WantAuthError c33sm2599297qtb.76 - gsmtp', ['[email protected]'])

Process finished with exit code 1
python smtp email-integration
1个回答
0
投票

该错误很容易解释。您需要提供身份验证。

server.login('<gmaiL-id>', '<password>')

将其放在server.starttls()之后

有关更多详细信息,请参考https://docs.python.org/2/library/smtplib.html#smtplib.SMTP.login

© www.soinside.com 2019 - 2024. All rights reserved.