A doctor in California who doled out highly addictive painkillers in exchange for sex has been sentenced to more than two years in federal prison.

Naga Raja Thota, a 62-year-old pain specialist based in El Cajon, while pleading guilty, admitted in November that he gave 870 oxycodone pills and 300 hydrocodone pills to a female patient. He also admitted prescribing pills in the names of the woman’s relatives in 2013 and having sexual contact with a second female patient in exchange for drugs in 2013 and 2014, the San Diego Union-Tribune reports.

“I think the doctor ignored the Hippocratic oath, ‘First do no harm,’” said US District Judge Larry Burns, who characterized Thota’s conduct as “egregious.”

Robert Schlein, Thota’s defense attorney, said his client enjoyed personal and professional success until his 29-year marriage fell apart, and asked the judge to consider leniency.

“There was no question he was at fault,” Schlein said, adding that many of the patients Thota handled at his practice were difficult to treat due to their addictions.

Prosecutors insisted that Thota knew his conduct was wrong, including the consequences of engaging in a sexual relationship with a patient.

“He absolutely knew it was wrong,” Assistant US Attorney Orlando Gutierrez said of the text message that Thota exchanged with one of the female patients.

Thota, meanwhile, said he was “deeply ashamed” about his actions. Schlein said he can no longer work as a doctor in the US and forfeited his medical license, the Union-Tribune reports.

The judge said he would recommend that Thota serve part of his 30-month prison sentence at a halfway house if approved by federal prison officials. He will also be on supervised release for five years.

Thota was arrested in August and charged with giving at least two young female patients prescriptions for opioids without legitimate medical reasons on several occasions in exchange for sex, Department of Justice officials announced last year.

“Doctors who exploit patients are the worst kind of predators,” DEA San Diego special agent in charge William Sherman said in a news release announcing the charges. “DEA recognizes the trust the citizens of San Diego place in their doctors. We will continue to ensure that physicians who are abusing that trust by bartering sex for prescriptions will be arrested and prosecuted.”