Skip to main content
 首页 » 编程设计

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

2024年10月17日7小虾米

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

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

请您参考如下方法:

你可以在 while 循环中使用 try and 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: ') 

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