Python script verify the mailbox id is a valid one ,Id, password provided

Viewed 24

Say i have a mailbox id: Dummy@cisco.com password: Dummy@123

I just want to verify the above mailbox is a valid one. I taught of logging into it and reading the connection message to get whether it's a valid mailbox Below is my following code

username = "Dummy@cisco.com"
password = "Dummy@123"

imap_server = "outlook.office365.com"

# create an IMAP4 class with SSL 
imap = imaplib.IMAP4_SSL(imap_server)
# authenticate
status,message = imap.login(username, password)
print(status)
print(message)

it gives the following error

Traceback (most recent call last):
  File "C:\Users\tochandr\mailtest.py", line 17, in <module>
    status,message = imap.login(username, password)
  File "C:\Users\tochandr\AppData\Local\Programs\Python\Python310\lib\imaplib.py", line 612, in login
    raise self.error(dat[-1])
imaplib.IMAP4.error: b'LOGIN failed.'

Not sure whats wrong Or is there any other way to verify the above mailbox is a valid one Any help is appriciated

1 Answers

If you have a valid username and password for the same server, you can open two connections at the same time, use the valid credentials in one while you use the credentials to test in the other, and then you compare the results:

  • If both succeed, then the credentials tested are valid
  • If the former succeeds and the latter not, then invalid.
  • If the former fails, then something is wrong and you don't know what.

Servers sometimes refuse valid credentials, for example because a backend is down or because you're trying to log in from a blocked IP address.

If you don't have valid credentials, you don't have a good way to distinguish between the various errors. You can parse the response codes defined by RFC 5530 if supplied, but those are often not supplied.

Related