Microsoft is moving away from IMAP and other basic authentication protocols so I would look at using modern authentication to authenticate. Effective October 1, 2022, they will be depreciating basic authentication and you will be forced to use modern authentication.
Hopefully this helps you on your quest to migrate to Microsoft 365.