python smtp 检查用户名和密码是否正确

python smtp check if username and pass are correct

你知道在使用 python 发送 smtp 邮件之前我如何检查用户名和密码是否输入正确吗?

含义:连接到smpt服务器,反馈用户名和密码

您可以在 while 循环中使用 try 和 except

#Inital user and passw need to be defined up here
log=False
while log==False: #to loop until a correct password is aqquired
    try:
        server.login(user, passw) #attempt to log into smtp server
        log=True #sets to true if log in is successful
    except:
        print('Incorrect Username or Password:\n')
        user=input('Enter new Username: ') #gets new username and password
        passw=input('Enter new Password: ')

这应该循环直到输入正确的用户名或密码, 然后它将登录