If you have multiple apple devices this becomes a problem. Mail has badges and can put email notifications in Notification Center. The emails sync fine, but the notifications of the mail so do not update until the app is opened. So if I read an email on my iPhone my iPad continues to show the badge that I have an unread email until I open the mail app on my iPad and it syncs with the IMAP server.
This is a serious problem with iCloud accounts and a somewhat lesser problem with most others. It's caused entirely by push notifications, and there's probably not much Apple can do about it for third-party accounts — at least not without draining your iPhone battery much more quickly.
There's only one mail provider I'm aware of that does this properly: Fastmail. That's because they implemented Apple's iCloud-style Push Notification system in 2015 — and, unlike Apple, Fastmail did it
right.
If you're using iCloud Mail, the default setting is to use push notifications for new mail messages. This allows you to get notifications of those messages on your iPhone (or iPad) as soon as they arrive in your inbox. Unfortunately, Apple only uses push notifications for
new messages. It doesn't push other mailbox changes at all. As a result, the badge count doesn't get updated until either another new message comes in or you open the Mail app manually to check for new mail.
Technically speaking, push notifications for Apple Mail don't update your badge count directly. Instead, they simply tell Apple Mail to go and check for mail in the background. That background process is what updates the badge count. iCloud Mail only sends out these notifications when a new message arrives, and hence Apple Mail on iPhone/iPad only check for new email in the background when new mail comes in.
You can set your iCloud account to "Fetch" under your Mail settings. The minimum interval is 15 minutes, but at least you won't have to live with a stale unread count all day. Fetch is also the default when used with other mail providers that don't support Apple's proprietary push notification system — which is almost all of them.
Originally, Apple Mail push notifications were only enabled by iCloud (MobileMe in those days) and Yahoo. It was also available on Apple's OS X Mail Server, but it worked the same way there as it did on iCloud — new mail only. I
hacked together my own solution to make the OS X Mail Server push updates in 2012, but I kind of gave up on that after
Fastmail joined the party in 2015, since they had also done it properly.
Fastmail doesn't just push unread messages, but also other mailbox updates like moving, flagging/unflagged, and deleting messages. It can even send push notifications for secondary folders so that they're always up to date. Again, there's not a lot of magic here — all Fastmail is doing is generating a notification to tell Apple Mail to do a background refresh whenever
anything changes in your mailbox. It would be trivially easy for Apple to do the same thing with iCloud, but for whatever reason it's chosen not to. There may be a scalability issue here, since this would significantly increase the number of push notifications coming out of Apple's servers, but I suspect the real reason is simply that Apple can't be bothered fixing it because most iCloud Mail users just don't care that much.
As an aside, this push notification system only applies to the iPhone and iPad. The macOS Mail app uses a different feature called IMAP IDLE to maintain a persistent connection to the mail server. That's why messages still come in instantly on your Mac from most mail providers. However, IMAP IDLE doesn't work well on a mobile device as it consumes a lot of power to keep the server connection open all the time and it expects your connection to be persistent, not regularly hopping between multiple Wi-Fi and cellular data networks.