I have this problem where when the user enters the wrong username and password, it would receive this error and crash (I am fairly new to python):

File "/Users/19austinh/Google Drive/Other/Python/Login.py", line 156, in <module>                user.login(euser, epass)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/smtplib.py", line 634, in login
    raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8 http://support.google.com/mail/bin/answer.py?answer=14257 vn10sm22434291pbc.21 - gsmtp')

Here is my code:

while True:
    user = smtplib.SMTP('smtp.gmail.com', 587)
    user.starttls()
    euser = input("Enter Gmail Username: ")
    epass = input("Enter Gmail Password: ")
    user.login(euser, epass)
    print ("Retrieving Files")
    subjectinput = input("Enter Subject: ")
    msginput = input("Enter Message: ")
    msg = ("\n"+msginput)
    sourcemail = (euser)
    targetmail = input("Enter Your Recipient: ")
    user.sendmail(sourcemail, targetmail, 'Subject: {}\r\n\r\n{}'.format(subjectinput, msg))
    print ("Email Sent")
    ask = input("Whould you like to send another one [Y/N]: ")
    if(ask == "n" or ask == "N" or ask == "no" or ask == "No"):
        break
    if(ask == "y" or ask == "Y" or ask == "yes" or ask == "Yes"):
        continue

So how do you make it so when the user enters the wrong username or password, it will go back and ask them for their credentials? Here is what I tried:

try:
    user.login(euser, epass)
    except (smtplib.SMTPAuthenticationError):
        print ("Invalid Credentials!")

However, this only says "Invalid Credentials!" but it lets the program keep running, thus crashing the script. I do not want to use "break" because this will exit out of "while True" and I do not want that to happen. Answers are greatly appreciated! Thanks :)

有帮助吗?

解决方案

Use nested while loop:

while True:
    user = smtplib.SMTP('smtp.gmail.com', 587)
    user.starttls()
    while True:
        euser = input("Enter Gmail Username: ")
        epass = input("Enter Gmail Password: ")
        try:
            user.login(euser, epass)
        except smtplib.SMTPAuthenticationError:
            print('Login failure: please reenter credential information.')
            continue
        break
    ...

其他提示

I think you're after the following construct so that the connection is created, you loop repeatedly until you login successfully, then you do your original send mail or not loop, eg:

user = smtplib.SMTP('smtp.gmail.com', 587)
user.starttls()
while True: # until we login
    euser = input("Enter Gmail Username: ")
    epass = input("Enter Gmail Password: ")
    try:
        user.login(euser, epass)
    except smtplib.SMTPAuthenticationError:
        print('Login failure: please reenter credential information.')
        continue
    break
while True:
    pass # Your original stuff using the connection
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top