China put on trial an Australian writer and academic accused of espionage Thursday in a case that could damage already deteriorating relations between the two countries.

Yang Hengjun, a novelist and former Chinese official who gave up his nationality and immigrated to Australia, was detained in early 2019 after taking a flight from New York to Guangzhou. He has had no access to family and only limited contact with his lawyer, the Australian government has said.

On Thursday, the Australian ambassador to China was turned away when he tried to attend Yang’s trial at Beijing No. 2 Intermediate People’s Court.

“This is deeply regrettable and concerning and unsatisfactory,” Ambassador Graham Fletcher told reporters outside the court. “We have had long-standing concerns about this case, including lack of transparency, and therefore have concluded it is an instance of arbitrary detention.”

The trial began a day after the release of a letter Yang dictated from detention in March, in which he said his health was deteriorating but his spirit was strong.

“I’ve been in confinement for 26 months now without fresh air or sunshine,” Yang said at the beginning of the message, which is addressed to an unnamed friend and was seen by The Washington Post.

“If the worst comes to the worst, if someone wants to take revenge on me for my writings, please explain to the people inside China what I did, and the significance of my writing to people in China,” continued Yang, who wrote spy novels about China and the United States as well as criticism of Chinese politics that was seen in intellectual circles as unvarnished yet relatively moderate.

A close family friend, Feng Chongyi, confirmed the letter’s authenticity to The Post, adding that the trial was “a mere formality and a shame.”

“It will be a closed-door trial,” said Feng, a professor at the University of Technology Sydney. “His family and Australian diplomats are not allowed to attend. There will be no cross-examination of witnesses.”

China has said little about the charges against Yang, other than that they relate to alleged espionage. He was held for seven months in “harsh conditions” before being charged in mid-2019, according to Australian Foreign Affairs Minister Marise Payne.

“I very much hope Dr. Yang is provided with a fair trial but we have not seen any explanation or evidence of the charges brought against him,” Payne told Australia’s ABC radio on Thursday.

Sophie McNeill, Australia researcher at Human Rights Watch, said it was “alarming” that Chinese authorities had prevented Australian diplomats from observing the proceedings. “Yang has been denied the right to a fair trial from the moment he was detained,” she said.

Chinese Foreign Ministry spokesman Zhao Lijian said Thursday that Beijing firmly rejected Australia’s efforts to intervene in Yang’s case.

This is not the first time Yang has been held in China, which he left in the 1990s following a stint at China’s Foreign Ministry. For years, he publicly joked that he was frequently recruited to be an asset for foreign intelligence. After he was detained for two days in 2011, Yang declined to publicly discuss the experience. But he wrote in a blog post that he would continue to work as a “calm intermediary” who pushed China — “our nation” — to become a strong, prosperous, free and democratic country.

The trial comes at a low point in relations between China and Australia. After Australia called for an investigation into the origin of the coronavirus pandemic, China blocked imports of Australian beef, wine, coal, lobsters, wood and barley. Rhetoric on both sides has ratcheted up recently.

Yang’s case is the latest in a string of incidents in which Beijing appears to detain foreign nationals as leverage in disputes with Western governments.

He was seized one month after China detained two Canadians, Michael Spavor and Michael Kovrig, who have since been arrested and charged with serious offenses. Their cases appear to have links to disputes between Beijing and Western governments over Huawei, the Chinese technology giant that is trying to become the world leader in 5G systems.

Last year, China accused Australian authorities of raiding the homes of Chinese journalists in Australia. A few weeks after the raids, Chinese authorities detained an Australian TV anchor working for a Chinese state-run broadcaster. She remains behind bars. Two other Australian correspondents spent days holed up in diplomatic compounds before fleeing China after Chinese authorities demanded to interview them over what they said was a national security matter.

In his letter, Yang wrote that if he were let out of prison, he would like to visit both Australia — “a Heaven-like place” — and “the green mountains and rivers in China.” He vowed to write articles “to improve Australia-China relations and that will help China to understand the world, and the world to understand China.”

But he also said he would not compromise his beliefs.

“There is nothing more liberating than having one’s worst fears realized. I have no fear now,” he said. “The values and beliefs which we shared, and which I shared with my readers, are something bigger than myself.”

Eva Dou contributed to this report.