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

时间:2016-07-07 11:20:25

标签: python smtp

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

含义:连接到smpt服务器,提供有关用户名和密码的反馈

1 个答案:

答案 0 :(得分:1)

您可以尝试使用while循环

#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: ')

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