Much admired and handsome actor James Garner was a Hollywood good guy who took a heartbreaking secret to his grave.

Garner – who won millions of fans in the ’50s as TV’s “Maverick” and then again in the ’70s on “The Rockford Files” – was found dead at his Los Angeles home on July 19 at age 86.

But for several years, his ready smile had hidden a long flirtation with death, The National ENQUIRER has learned exclusively.

“More than once in his life, Jim thought about killing himself,” disclosed a close friend. “He suffered bouts of depression, which would cripple him.”

The popular star admitted to smoking marijuana to ease his sadness and once confessed, “I’ve had times when I couldn’t make a decision whether to take a shower or a bath so I didn’t do either and just sat there – for days.”

While the actor’s rep said he died of natural causes, his friend disclosed: “What really killed him were two recent strokes he’d suffered, the last one not long before his 85th birthday.”

The ENQUIRER learned that the easy-going actor endured a lot of pain in his life, caused by his endless drive for perfection in his work.

The strapping 6-foot-3 star suffered injuries from doing his own stunts, which later forced him to have two knee replacements, and left him with arthritis in his back and hands.

Eventually, Garner left his role as struggling private investigator Jim Rockford, and in 1980 he told The ENQUIRER in an exclusive interview: “I didn’t run away from the ‘Rockford’ show. I was sick and needed to get well. It’s terribly long hours, and after six years you look back. David Janssen ended up in the hospital half the time and so did Jim Arness. It’s extremely difficult work.”

His marriage was also difficult – and his wife, Lois, was reportedly tested by his alleged affairs.

Garner met Lois at a political rally in 1956, the year before he started filming “Maverick,” and they tied the knot 14 days later. The big-hearted star adopted his wife’s 8-year-old daughter, Kimberly, and the couple went on to have a daughter, Greta, two years later.

In 1979, The ENQUIRER reported that the Garners had separated, but they eventually got back together. For 57 years until Garner’s death, the marriage survived his rumored affairs with actress Lauren Bacall and many other starlets on the Hollywood scene.

“James considered leaving Lois on numerous occasions,” confided a pal. “But in the end, he could never leave her – he didn’t have the heart.”