I got that error, credentials is ok and was working in the morning. Reset password did not change, logging into OWA works fine, login using imaplib fails with "LOGIN failed" after 1 minute or so!
def get_otp():
# sleeping for 20 seconds
time.sleep(20)
# username for mail id
user = '***********'
# password for email id
password = '********'
# hostname for webmail.recogx.ai
hostname = 'mail.office365.com'
print("entered hostname ")
def get_body(msg):
# id=f message is present in more than one part
if msg.is_multipart():
return get_body(msg.get_payload(0))
else:
return msg.get_payload(None, True)
print("bodyyyyyy")
# entering hostname to enter into session
# time.sleep(10)
mail = imaplib.IMAP4_SSL(hostname)
time.sleep(5)
print("logged ")
# logging in page
print("mail login",mail.login(user, password))
mail.login(user, password)
print("logged in")
# selecting inbox
mail.select('INBOX')
# searching unseen messages
result, data = mail.search(None, 'UNSEEN')
# finding total mail numbers
mail_ids = data[0]
id_list = mail_ids.split()
latest = id_list[-1]
print("opt next")
# fetching data from latest email
result, data = mail.fetch(latest, '(RFC822)')
ror = email.message_from_bytes(data[0][1])
body = get_body(ror)
# converting text body from byte to string
body = body.decode("utf-8")
time.sleep(3)
otp = re.search(r'\d{7}', body).group()
Unable to read the OTP.