TL;DR: Use imapsync with DavMail.

So my subscription for Office 365 (or whatever it's called these days) was due for renewal in January. I only ever used it for email on my domain. Given how increasingly hateful the Outlook web client has been in recent years (worst among them was when it randomly disabled the ability to send plain-text email for some time), and my general desire to move as much of my life away from software I have no control over, I decided to migrate my shit to Migadu. Partly because Migadu is kinda cheap, partly because of a good recommendation, but mostly because I really like their beautifully simple and functional website. I'm shallow!

Obviously, I didn't want to lose any of my email in this process. What should have happened is that I would enable IMAP on Office365, plug in some command line options to imapsync, and by the magic of open standards email would be moved from one place to another. Open standards are good! People who write open source software are awesome!

Of course, one half of that involved dealing with Microsoft. That and the rule of time estimates (increase the unit, halve the quantity) and the rule of everything is bullshit and never fucking works meant that I wasted more than a few hours trying to make that happen. Greetings from 3am!

The short version is if you use Office 365 in the same manner I did, which is that you were both the administrator of the domain and a user on the domain (because it was for personal email and you're the only person on the domain), nothing you do will make IMAP work for your account. Not if you navigate six levels of Enterprise-ness and a UI with decade-old branding to enable 2FA and then to enable "app passwords", not if you go disable security defaults and probably still nope if you run some PowerShell magic spells that I've seen around.

Give up trying to make IMAP work directly and use DavMail. I did!

DavMail is a proxy between Microsoft's proprietary protocols and protocols such as IMAP that the rest of the world uses. There's a package in the Ubuntu repositories called davmail, but because everything is bullshit and never fucking works the Ubuntu package is broken if you don't install the openjdk-11-jre package first (through no fault of DavMail's authors).

Anyway, once you've worked out that last bit from reading some random Debian bug report, you will want to go into DavMail's settings, under the "Main" tab, and change "Exchange Protocol" to "O365Interactive".

Then run a magic spell...

./imapsync \
  --host1 localhost \
  --port1 1143 \
  --user1 'you@dealingwithbullshit.com' \
  --debugimap1 \
  --password1 'hunter2' \
  --host2 'imap.migadu.com' \
  --user2 'you@dealingwithbullshit.com' \
  --password2 'hunter3'

...and DavMail will pop up a with a link to open in your browser, and that will in turn redirect to a URL that you copy-and-paste into DavMail. Some time later your mail will appear in Migadu or whatever else you decide to migrate to.

So, that was some bullshit. But it's done now! And I only have to do that once. And if I ever do have to do it again I won't waste hours of my time and hours of someone else's time trying to work out why IMAP isn't working, because, reminder, if your account is an admin account for an O365 domain it absolutely never will.

Anyway, imapsync and DavMail are good! They made me happy! I've only been dealing with mail migration bullshit for one long night, but the authors of both have been dealing with it for years. Here's a figurative toast to those fine people, and here's to many years of Migadu and me!