为了账号安全,请及时绑定邮箱和手机立即绑定

凭据问题(google smtp)

凭据问题(google smtp)

慕莱坞森 2021-11-02 20:03:25
我是一个非常新的Python 编码员,所以请不要对我太苛刻,谢谢。我正在尝试使用电子邮件发送器smtplib,但在将用户凭据交给 Google 时遇到了问题。完整代码:mailcheck = input("This mailer is only for gmail, want to continue? (yes or no)")# GMAIL SMTP INFORMATIONif mailcheck == "yes" or mailcheck == "Yes" or mailcheck == "y":    smtp_server = 'smtp.gmail.com'    port = 587    set_server = "gmail"else:    print("Hey, sorry this program is only for Gmail")#USER CREDENTIALS + RECEIVERusername = input("Google Email?")password = input("Password?")receiver = input("Who to send it to?")subject = input("Subject of the email?")econtents = input("What to put in the email?")amount = input("Amount of emails to send?")credentials = username + passwordglobal d1global d2try:    server = smtplib.SMTP(smtp_server, port)    server.ehlo()    server.starttls()    server.login(username, password)    print("Sending" + amount, "to", receiver)    for i in range(1, int(amount) + 1):        message = "From" + credentials[0] + '\nSubject' + subject + '\n' + econtents        time.sleep(random.uniform(d1, d2))        server.sendmail(credentials[0], receiver, message)        print("\nEmail Sent!")    else:        print("Finished sending emails")except smtplib.SMTPRecipientsRefused:        print("Recipient refused. Invalid email address?")except smtplib.SMTPAuthenticationError:        print("Unable to authenticate with server. Ensure the details are correct.")except smtplib.SMTPServerDisconnected:        print("Server disconnected for an unknown reason.")except smtplib.SMTPConnectError:        print("Unable to connect to server.")错误 :Unable to authenticate with server. Ensure the details are correct.我认为这是因为credentials = username + password哪个不起作用,但我不知道如何解决它。如果有人知道我必须改变什么才能解决这个问题,那就太好了!
查看完整描述

1 回答

?
ABOUTYOU

TA贡献1812条经验 获得超5个赞

您不是将这两个字符串相加,而是将它们放入一个数组中。在 Python 中,要么


credentials = [username, password]

或者


credentials = list(username, password)

但这似乎不是你的问题。当您收到 SMTPAuthenticationError 异常时,您的问题与 login() 函数有关。smtplib 文档说在运行 .starttls() 之后,您应该再次运行 .ehlo()。尝试在登录之前运行它。此外,您可以尝试在端口 465 上生成 SSL 实例。


(Ctrl+F 表示 .starttls())


https://docs.python.org/2/library/smtplib.html


查看完整回答
反对 回复 2021-11-02
  • 1 回答
  • 0 关注
  • 307 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信