无法使用smtp登录

2024-09-29 02:21:48 发布

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

我输入了正确的电子邮件/密码,但它仍然无法登录到该邮件/无法使用该邮件发送消息,因此它输出了您输入的用户名或密码不正确有人能帮我确定问题并提出一个好的解决方案吗

多谢各位

-我

def Emailspam():
print("Choose your email provider")
print("1. Gmail")
print("2. Outlook")
provider = input(CRED + ">>> " + CEND)
os.system("cls")

useremail = input("EMAIL : ")
userpass = input("PASSWORD : ")
os.system("cls")

victimemail = input("Victim email : ")
Content = input('Message : ')
Number = int(input("Number of mail to send : "))
os.system("cls")

#https://www.androidauthority.com/gmail-smtp-settings-801100/#:~:text=SMTP%20server%20address%3A%20smtp.gmail,SMTP%20port%20(SSL)%3A%20465
if provider == ("1"):
    smtp_server = 'smtp.gmail.com'
    port = 587
#https://support.microsoft.com/en-us/office/pop-imap-and-smtp-settings-for-outlook-com-d088b986-291d-42b8-9564-9c414e2aa040
elif provider == ("2"):
    smtp_server = 'smtp.office365.com'
    port = 587

else:
    print("Invalid choice")
    skrr = input("press enter to close")
    exit()

try:
    server = smtplib.SMTP(smtp_server,port)
    server.login(useremail,userpass)

    for i in range(0,Number):
        print("Number of Message Sent to " + victimemail + ":" , i+1)
        server.starttls()
        server.ehlo()
        server.sendmail(useremail,victimemail,Content)
        time.sleep(1)

    server.close()

except Exception as e:
    print('The username or password you entered is incorrect.')
    p = input("Press enter to close")
    exit()

Tags: tocomnumberinputserverosportprovider
3条回答

使用server.connect并将一些东西移到循环之外可以解决此问题

 def Emailspam():
        print("Choose your email provider")
        print("1. Gmail")
        print("2. Outlook")
        provider = input(CRED + ">>> " + CEND)
        os.system("cls")
    
        useremail = input("EMAIL : ")
        userpass = input("PASSWORD : ")
        os.system("cls")
    
        victimemail = input("Victim email : ")
        Content = input('Message : ')
        Number = int(input("Number of mail to send : "))
        os.system("cls")
        
        #https://www.androidauthority.com/gmail-smtp-settings-801100/#:~:text=SMTP%20server%20address%3A%20smtp.gmail,SMTP%20port%20(SSL)%3A%20465
        if provider == ("1"):
            smtp_server = 'smtp.gmail.com'
            port = 587
        #https://support.microsoft.com/en-us/office/pop-imap-and-smtp-settings-for-outlook-com-d088b986-291d-42b8-9564-9c414e2aa040
        elif provider == ("2"):
            smtp_server = 'smtp.office365.com'
            port = 587
        
        else:
            print("Invalid choice")
            skrr = input("press enter to close")
            exit()
    
        try:
            server = smtplib.SMTP(smtp_server,port)
            server.connect(smtp_server,port)
            server.ehlo()
            server.starttls()
            server.login(useremail,userpass)
    
    
            for i in range(0,Number):
                print("Number of Message Sent to " + victimemail + ":" , i+1)
                server.sendmail(useremail,victimemail,Content)
                time.sleep(1)
    
            print("Finished")
            server.close()
    
        except Exception as e:
            print(e)
            print('The username or password you entered is incorrect.')
            p = input("Press enter to close")
            exit()

尝试创建应用程序密码,然后查看它是否有效。要创建应用程序密码,请转到您的帐户并进入隐私设置进行设置。此外,请尝试添加:

server.ehlo()

之后:

server.starttls()

谷歌已经提高了他们的安全性。因此,如果你没有从你的gmail帐户关闭安全性较差的应用程序访问,你将无法登录任何gmail帐户。要做到这一点,请首先单击管理谷歌帐户>;安全选项卡>;不太安全的应用程序访问>;关这是它的一篇文章:How to turn off less secure app access in gmail

另一件事是,您不应该将登录代码保留在for循环中。您应该在for循环之外登录。因此,你将登录一次,并发送电子邮件与该帐户多次

希望能有帮助

以下是工作代码:

def Emailspam():
    print("Choose your email provider")
    print("1. Gmail")
    print("2. Outlook")
    provider = input(CRED + ">>> " + CEND)
    os.system("cls")

    useremail = input("EMAIL : ")
    userpass = input("PASSWORD : ")
    os.system("cls")

    victimemail = input("Victim email : ")
    Content = input('Message : ')
    Number = int(input("Number of mail to send : "))
    os.system("cls")


    if provider == ("1"):
        smtp_server = 'smtp.gmail.com'
        port = 587

    elif provider == ("2"):
        smtp_server = 'smtp.office365.com'
        port = 587

    else:
        print("Invalid choice")
        skrr = input("press enter to close")
        exit()

    try:
        server = smtplib.SMTP(smtp_server,port)
        server.ehlo()
        server.starttls()
        server.login(useremail,userpass)

        for i in range(0,Number):
            print("Number of Message Sent to " + victimemail + ":" , i+1)
            server.sendmail(useremail,victimemail,Content)
            time.sleep(1)

        server.close()

    except Exception as e:
        print('The username or password you entered is incorrect.')
        p = input("Press enter to close")
        exit()

相关问题 更多 >