Replace IMAP library by Graph API
Try to use Graph API instead of IMAP to retrieve mails. IMAP being half broken on 365.
https://kontext.tech/article/843/read-email-from-microsoft-365-via-microsoft-graph-api
https://learn.microsoft.com/en-us/graph/api/message-get?view=graph-rest-1.0&tabs=python
https://github.com/domainaware/parsedmarc/blob/master/parsedmarc/mail/graph.py