Mail confused by IMAP and loses messages
I have been encountering a strange bug in Mac mail for some time now. I use the Mail program on my Mac to access my iCloud mail as well as mail from other IMAP servers. I also access these IMAP mail accounts from other devices, including my iPhone. When use the mac to delete messages, they disappear from the inbox as expected. However, sometimes when new messages arrive, Mail on the Mac displays one of the deleted messages as new instead of downloading the real message from the IMAP server. I confirm this by going to my other device (such as my iPhone) to see the actual new message that came in. This same problem happens on the mac for multiple IMAP accounts, not just for iCloud. Is there something I can do to get Mail on the Mac to actually get the new messages rather than displaying the last deleted message as if it were new?
MacBook Pro, macOS Sierra (10.12.6)