Mail.app has a feature called rules that enables you to automate your mail tasks.
You can define rules in the Mail preferences.
Here's what I'd do:
NOTE: I tend to rename folders and prefix them with a '#' instead of moving them. Do whichever you'd prefer.
Delete gmail accounts from mail.app.
Close mail and go to ~/Library/Mail. Move it somewhere (delete it later if all goes well).
Go to ~/Library/Containers/, and move com.apple.mail.
Go to ~/Library/Preferences/, and move com.apple.mail.*
Run Mail, and set your accounts up. Go to Mail -> Preferences -> Accounts.
For each gmail account:
Turn off Junk Mail.
Give them a good name so you can tell them apart.
In the SMTP settings, make sure 'use only this server' is checked.
In the SMTP settings -> Edit SMTP server list -> give it a good description, change the port to 587, make sure credentials are filled in and SSL is checked.
Mailbox behaviours - all checked except junk mail. I set dropdown boxes to never, never, and one month old.
Advanced - IMAP path prefix should be '[Gmail]', IDLE checked.
I tend to also go to Viewing, and set it back to classic layout. I like enabling the bold unread messages. Then I'll customize my columns.
Finally, once your email begins to download you'll need to map the sent/drafts/trash folders properly. Send an email and a 'Sent Messages' folder will be created if it doesn't already exist. This is the one Mail uses but not the one gmail/iPhone will use. So click on Sent Mail, and go to the Mailbox menu -> Use this Mailbox for ... -> Sent.
Do the same with Trash and Drafts.
Now if you do have the sent messages folder, I recommend logging in to the gmail web interface at gmail.com, and just move all of those messages to the correct sent folder. Takes about 2 seconds in gmail.com, but will take forever if you try and do the same thing from mail.app.
Wait for everything to sync up, test sending/receiving from your computer, iPhone, iPad, etc. Make sure the mappings are working out and everything goes where its supposed to.
If you get sick of mail.app, go and buy MailMate (http://freron.com), which is my new favourite mail client.
Best Answer
Make sure that all the permissions are correct on your home folder.
You can use the ACL tool on the Recovery HD to do this:
resetpassword
in Terminal (available from Utilities → Terminal).