I assume you don't keep your iMac in a location that gets that hot, so does your iMac actually shut itself off when it is warmed up?
The power supply on your iMac is known to be a little fragile. so you should try simply replacing the power supply. Apple may do this at no charge.
Even if you pay for the repair, the power supply is not too expensive, and is a 5-minute job to replace.
You should also check the air intake area all along the bottom edge of your iMac. Make sure that area is not plugged with debris. I see some of those models that are completely coated with lint and dust. A little vacuum brush under that area sometimes makes a big difference.