Also verify that the server supports SSL. Check your network connection and that you entered the correct information in the Account preferences.
In the Apple Mail > Connection Doctor, the error said: Could not connect to this Google IMAP server. Mail would then complain that port 143 was not accessible. I checked all the obvious things, and nothing worked. It stated that port 993 could not be connected to. After working flawlessly for a few years, since that day a connection error was coming up. Since June 15th Apple Mail ceased connecting to her two Gmail accounts over IMAP. Today a friend came to me with an odd issue. I help a few friends with their Mac computers (well, and Windows and Linux computers too).