Kevin Mallory, a former CIA officer, was $230,000 in debt and months behind on his mortgage in early 2017, court records show, when he received a LinkedIn message from a Chinese headhunter.

The exchange set off a chain of events that led to Mallory's conviction last year on charges of spying for China. He's expected to be sentenced later this month in Virginia. Mallory, 61, is facing up to a life term.

Prosecutors say his betrayal was particularly egregious because he gave his Chinese handlers highly classified information about American operatives who were due to travel to China. Mallory's sentencing comes amid increasing alarm among U.S. intelligence officials about the breadth and intensity of Chinese espionage against the United States — through hacking, recruitment of American agents and infiltration of Chinese operatives into U.S. companies and organizations.

Still frame from CIA interrogation video of Kevin Mallory. Department of Justice

On Tuesday, a Chinese woman was arrested after talking her way into President Donald Trump's Mar-a-Lago compound in Florida with a thumb-drive containing software that authorities fear could be used to secretly penetrate computer networks.

Last month, a former officer for the Pentagon’s intelligence arm, the Defense Intelligence Agency, pleaded guilty to charges of attempting to spy for China. Last year, another former CIA officer, Jerry Chun Shing Lee, was indicted on espionage charges. NBC News has reported that authorities suspect he provided information that caused the deaths of more than a dozen CIA informants in China.

When experts assess the top security threat facing the U.S., “I think it’s all China, all the time,” former CIA Director and retired Gen. David Petraeus said recently during a gathering of current and former intelligence officials in Sea Island, Georgia.

Mallory's was the rare spying case that went to trial, and as a result there is a significant public record about what happened. It was a classic spy recruitment of someone whose career had floundered, leading to money troubles.

A graduate of Brigham Young University, Mallory spent a stint in the U.S. army and a career in the intelligence world.

He worked as a CIA case officer in the 1990s, meaning he was an undercover operative, tasked with handling agents and stealing foreign secrets. Fluent in Mandarin, he became a senior intelligence officer for the Defense Intelligence Agency in the early 2000s. He returned to the CIA as a contractor from 2010-2012, but he lost his top secret security clearance after improperly disclosing classified information, court records show.

He didn’t have a steady job after that, court records say, which made him a ripe target for recruitment.

"As of January 17, 2017, defendant was $12,205.32 past due on his mortgage payments,” court records say. He had $30,000 in credit card debt and a balance of more than $200,000 on a home equity line of credit.

In February 2017, Mallory responded to the LinkedIn message from Michael Yang, who passed himself off as a think tank representative looking for a foreign policy expert. The FBI says he was a Chinese intelligence officer.

The two struck up a relationship, records show, and Mallory eventually was paid $25,000. He took two trips to China and handed over important secrets, according to trial testimony. His handlers gave him a Samsung phone that doubled as a covert communications device, according to testimony.

As he worked with the Chinese, Mallory began reaching out to former CIA colleagues in an apparent effort to pump them for information. The ex-colleagues grew suspicious and contacted the CIA, prosecutors say.

The spy agency launched an investigation with the FBI. Customs officers stopped Mallory at Chicago's O'Hare Airport as he returned from his second trip to China. Agents discovered $16,000 in cash and the phone.

The device proved to be a goldmine for prosecutors.

"Your object is to gain information," Mallory told Yang, according to texts recovered from the phone, "and my object is to be paid for."

He added: "I will destroy all electronic records after you confirm receipt. I already destroyed the paper records. I cannot keep these around, too dangerous."

Once he realized authorities were on to him, Mallory reported to the CIA that he believed Chinese intelligence had tried to recruit him. He seemed stunned, according to court testimony, when the incriminating messages popped up on the Samsung phone in the presence of investigators.

After they arrested him, the FBI searched his home and found a digital storage card with eight secret and top-secret documents.

Jurors were told those documents disclosed details of a still-classified spying operation and a CIA analysis of another foreign country’s intelligence capabilities.

“The information itself still remains current and revelatory,” Hugh Michael Higgins, former head of operations at the DIA, testified in court. “It would still have a severe impact on national defense.”

Some information on the documents could have put American agents in danger, prosecutors said. And Mallory sent the information about the couple working on behalf of the Defense Intelligence Agency after he learned they would be traveling to the China in the summer of 2017, according to court records.

A federal jury convicted Mallory in June 2018.

The judge in the case is T.S. Ellis, who was criticized for what some called a relatively light sentence he handed to former Trump campaign chairman Paul Manafort.

Mallory has never acknowledged his guilt, prosecutors say, and his lawyers have argued for a lesser sentence, saying the case led to no real national security damage.

Prosecutors see it much differently.

The "defendant, who was entrusted with our nation’s critical secrets, put our country and human lives—including the lives of assets—at risk for financial gain," prosecutors said in their sentencing memo. "He then lied about his actions and took steps to conceal them. Accordingly, the nature and circumstances of the offenses amply demonstrate why a sentence of life imprisonment is appropriate."