Depends. There are several methods.
Run a ground wire
Note that receptacles have a metal yoke that hold the mounting screws. This yoke typically has "drywall ears" to hold the socket even with the drywall surface. If all these are true:
- The junction box is metal, and grounded
- The receptacle's yoke, when screwed down, has good, screwed-down contact with the metal of the junction box (not held proud of it by the drywall ears)
- The yoke and junction box are bare metal (not coated with paint, gunk or rust)
- The mounting screw does not have a non-conductive paper square on it to capture the screw and interrupt electrical contact
They make metal spacers to solve the "proud of the drywall" problem, and allow you to tick the second box. You still need to tick the rest.
These are receptacles with a tricky metal wing in relation to the mounting screw(s), which takes some extra steps to try to solve the "gap" and "paper square" problems above. You still need to conform with all of the above, e.g. metal box.
Note there is nothing wrong with the receptacle being held proud of the box; actually you want the drywall ears to do their job and hold the receptacle even with the wall, so the cover plate will fit properly. It's just that if it is held proud, you have to run a ground wire (or use a self-grounding receptacle).