Please quit Mail. Force quit if necessary.
Back up all data before proceeding.
If you're running OS X 10.11 ("El Capitan") or later, triple-click anywhere in the line below on this page to select it:
~/Library/Mail/V3/MailData
If you're running an older version of OS X, use this line:
~/Library/Mail/V2/MailData
Copy the selected text to the Clipboard by pressing the key combination command-C. In the Finder, select
Go ▹ Go to Folder
from the menu bar. Paste into the box that opens by pressing command-V, then press return.
A folder window will open. Inside it there should be files with names as follows:
Envelope Index
ExternalUpdates.storedata
Move those files to the Desktop, leaving the window open. Other files in the folder may have longer names that begin as above. Move those files, if any, to the Trash.
Relaunch Mail. It should prompt you to re-import your messages. You may get a warning that the index is damaged and that Mail has to quit. Click OK. Typically, the process takes a few minutes, but it may take hours if you have gigantic mailboxes. In that case, you may be able to speed things up by temporarily adding your home folder to the Privacy list in the Spotlight preference pane. Remove it when Mail has finished importing.
If the import operation stalls or fails with an error message, quit Mail, delete the two new index files (keeping the original ones that you moved to the Desktop), and try again. According to many reports, the second attempt may succeed after the first one has failed.
Test. If Mail now works as expected, you can delete the files you moved to the Desktop. Otherwise, post your results.