I like Apple’s Mail.app for reading email. It doesn’t drag down as much as Microsoft Entourage, which uses one monolithic file to store all your email. I say monolithic because when I was last using it, it was over 1 Gigabyte in size (yes, I get a lot of email). With that said, there is an issue that comes up occasionally, and that is that the database Mail.app uses to track email’s status can get corrupted (especially after a crash of Mail.app). Some of the symptoms I have seen:

  1. Typically the start of the problems involve Mail.app misreporting the number of unreads in certain mail directories.
  2. Mail.app looses track of emails I know I have received.
  3. Clicking on an email pulls up a message about that email no longer being available or somesuch nonsense.
  4. In some cases, Mail.app starts crashing much more often. This is what actually caused me to start looking at the Console logs and notice that Mail.app was throwing errors about mailboxes with “negative” numbers of emails within them.

I have found in these situations, I can fix the problems by doing the following in order to rebuild the database.

  1. WARNING: This solution involves deleting files and could leave your email totally destroyed and you without a way to recover. You should darn well BACKUP the “~/Library/Mail” folder before proceeding [you do make backups regularly, don't you?].
  2. I first make screen capture images of my “Unread Messages” and “Flagged Messages” smart folders in Mail.app. Rebuilding the database seems to find messages that were not marked as unread and properly labels them unread. So these snapshots provide an easy way to record what I actually thought was unread. If you don’t have those smart folder built, I suggest building them… smart folders are actually a slick feature.
  3. I then quit Mail.app and move the “~/Library/Envelope Index” file to the desktop.
  4. I relaunch Mail.app, this will prompt Mail.app to “Reimport the email”. This is the process by which it appears to rebuild its database.
  5. Several minutes later, when it is done, I look at my “Unread Messages” and “Flagged Messages” smart folders and “read” or “unflag” any messages that I had previously marked as read and/or flagged.
    • I have noticed that often there will be duplicate copies of the emails left over at this stage. I suspect that when email was moved from my “Inbox” to the folder, the copy in the Inbox didn’t get deleted properly, so it gets recovered in the database rebuild. I have not confirmed this, but it does seem to happen a lot.
  6. Once you are convinced everything is OK, delete the “Envelope Index” file you placed on your desktop.

And that’s it. You mileage may vary, so don’t blame me if this explodes in your face.