For the spy agency to “directly interfere with the free expression of ideas by the people with the aim of creating a certain public opinion cannot be tolerated under any pretext,” the court said in its ruling on Thursday. “This is a serious crime that shakes the foundation of democracy.”

But though Mr. Won was convicted of violating the law governing the spy agency, the court dismissed a separate charge: that he had violated the country’s election law, which prohibits public servants generally from interfering in elections. In explaining that decision, the court said Mr. Won had not ordered his agents to support or oppose any specific presidential candidate.

That finding spared Ms. Park a potentially serious political liability. Had Mr. Won been convicted of violating the election law, it would have provided fodder for critics of Ms. Park who say that the agency’s online smear campaign undermined the legitimacy of her election. Ms. Park, who was elected by a margin of about a million votes, has said that she neither ordered nor benefited from such a campaign.

Two other former senior officials of the spy agency who had been indicted on similar charges were each sentenced to a year in prison on Thursday, but their sentences were also suspended. Both the prosecutors and the defendants have a week to appeal the verdicts.

The intelligence service has denied trying to discredit opposition politicians, saying that its online messages were posted as part of a normal campaign of psychological warfare against North Korea. It said the North was increasingly using the Internet to spread misinformation in support of the Pyongyang government and to criticize South Korean policies, forcing its agents to defend those policies online.