I'm having difficulty answering your question because I have fundamental concerns about your overall system.
The reason why IMAP is disabled (by default) in services like Office 365 is because of weak authentication. Something like 99% of account compromises occurs through basic authentication via POP3, IMAP and SMTP authentication.
And then, there is the use of on-premise exchange. One of the most compromised platforms out there.
If I were looking at the big picture, I would say migrate to Office 365; then you can use the built-in secure OUATH2 support built into Bitrix24.
https://helpdesk.bitrix24.com/open/16697760/
I do not believe you will be able to implement a solution to do what you have described that won't result in an account compromise within 6 months.
I wish I had something more positive to offer.