简体   繁体   中英

imaplib.error: b'LOGIN failed' when trying to login using imaplib in python

Good day, I am trying to use imaplib to connect to exchange server. Below is the code:

import imaplib

imap_host = 'example.smth.smth.kz'
mail_login = 'name.lastname@smth.kz'
mail_pass = '**********'


M = imaplib.IMAP4_SSL(imap_host)
print(M.welcome)
M.login(user1, imap_pass)

M.Welcome returns b'* OK The Microsoft Exchange IMAP4 service is ready.' , which means that I was able to connect to the server. However, when I try to log in I got this error: imaplib.error: b'LOGIN failed' Altough, credentials are correct. What is the issue here?

Your mail login username and password are incorrect according to your imap4 mail host.

When I look at your code, there is no user1 and imap_pass variable. If you want to write your given code correctly it should look like this:

import imaplib

imap_host = 'example.smth.smth.kz'
mail_login = 'name.lastname@smth.kz'
mail_pass = '**********'


M = imaplib.IMAP4_SSL(imap_host)
print(M.welcome)
M.login(mail_login, mail_pass)

Maybe use getpass to prevent passwords in your code:

import imaplib
import getpass

imap_host = 'example.smth.smth.kz'
mail_login = input("Username: ")
mail_pass = getpass.getpass()

M = imaplib.IMAP4_SSL(imap_host)
print(M.welcome)
M.login(mail_login, mail_pass)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM