凭据有问题(google smtp)

2024-10-05 12:27:16 发布

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

我是一个非常新的Python程序员,所以请不要对我太苛刻,谢谢

我正在尝试使用smtplib创建一个emailer,但在将用户凭据交给Google时遇到了问题

完整代码:

mailcheck = input("This mailer is only for gmail, want to continue? (yes or no)")

# GMAIL SMTP INFORMATION
if 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 + RECEIVER

username = 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 + password
global d1
global d2

try:
    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.

这意味着登录过程出错。这部分的某些地方应该出了问题:

 #USER CREDENTIALS + RECEIVER

username = 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 + password
global d1
global d2

try:
    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!")

我想这是因为credentials = username + password不起作用,但我不知道如何修复它

如果有人知道我必须改变什么来修复这个问题,那就太好了!p>

Tags: thetoforinputserveremailusernamepassword
1条回答
网友
1楼 · 发布于 2024-10-05 12:27:16

不是将这两个字符串相加,而是将它们放入一个数组中。在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

相关问题 更多 >

    热门问题