The emails, sent from Mr. Hynes’s official district attorney email account, illustrate how a veteran prosecutor came to be consumed by his efforts to be re-elected, a campaign that grew desperate as it became clearer that his political career was about to end.

The emails, obtained by The New York Times, were among thousands reviewed by the city’s Department of Investigation in an inquiry that concluded that Mr. Hynes could face larceny charges for spending public funds on his campaign. Mr. Hynes lost to Mr. Thompson in both the Democratic primary and the general election.

Mr. Hynes’s mood about the 2013 election moved from upbeat to insecure, according to a new batch of emails cited in appendices to the report.

During that time, the pressure in his office was increasing. Victim advocates were upset about how the office was handling sex-abuse cases involving ultra-Orthodox Jews. A panel was about to review dozens of convictions involving a discredited police detective. But the emails cited in the report show that the campaign was constantly at the top of his mind.

The potential larceny charges stemmed from the improper use of asset forfeiture funds to pay a longtime associate, the public relations consultant Mortimer Matz, for what the report concluded was campaign work. The report also concluded that Mr. Hynes might face misdemeanor charges for using his office email address and resources to conduct his campaign.

The report also cited a well-regarded judge, Barry Kamins, for breaching the judicial code of conduct by offering political advice to Mr. Hynes. Justice Kamins had made his career as a lawyer defending judges accused of ethical breaches.

Justice Kamins was relieved on Monday of his duties as chief administrative judge for New York City’s criminal courts and the head of policy and planning for New York State’s courts.

Mr. Hynes could not be reached for comment. A lawyer for Justice Kamins said his client had merely “talked politics” with Mr. Hynes, an old friend.

It was not until the summer of 2012 that Mr. Hynes seemed to become aware that he would need to run for re-election in a serious way. He had seen a big challenge only once before — in 2005, when he narrowly beat John L. Sampson in the primary — in his nearly two dozen years in office.

He began keeping an eye on Mr. Thompson and Abe George, who would run against him in the Democratic primary. And in June 2012, he emailed Justice Kamins about a rumor that Mr. George would soon announce a campaign. “He has a steep hill to climb but he could potentially quickly become the darling of much of the print media,” Mr. Hynes wrote.

“Even though it is doubtful he will have a strong funding source it’s clear he has to be taken seriously,” Justice Kamins asked. “How large is the Indian community in Brooklyn?” (Mr. George is Indian-American.)

“I will take him seriously,” Mr. Hynes responded. “Indian population is a sliver.”

In November, Mr. Hynes reported to his campaign manager that Mr. Thompson and Mr. George had attended a victory party for Hakeem Jeffries, who had just won a House seat representing Brooklyn and Queens. “We have to make it clear that our campaign is in gear,” Mr. Hynes wrote.

Mr. Hynes was still confident in March 2013, as suggested by an email exchange he had with Justice Kamins over a New York Law Journal article on the race. “George’s unsupported claim that people of Brooklyn have lost confidence is belied by the positive reaction I get on the ground,” Mr. Hynes wrote. “I’m not sure I understand Thompson’s strategy. Overall I think I benefit from the ancient adage that you can’t beat someone with no one.”

A highly publicized case over a conviction Mr. Hynes had obtained that had been overturned, that of Jabbar Collins, who was represented by the lawyer Joel B. Rudin, particularly rankled him. “Rudin is out of control,” Justice Kamins wrote.

“Yes he is and the Reporters are his stenographers,” Mr. Hynes responded.

A Hynes endorsement by the Shirley Chisholm Democratic Club prompted another reference to clowns by Mr. Hynes. “Another African-American Club cuts out the clowns!” he wrote to Justice Kamins.

He was getting snippy with other allies, though. As a consultant complained about the difficulties in drumming up positive press, Mr. Hynes wrote, “That is a problem that you have to deal with. I am not running this campaign. My role limited to campaigning.”

After a profile of Mr. Hynes appeared in The New York Times, a friend and former judge on the New York State Court of Appeals, Joseph W. Bellacosa, sent him an upbeat note. “Love the Times profile. Looks, sounds, feels and smells like a confident winner. As they say in Vegas — the House always wins.”

Mr. Hynes forwarded Judge Bellacosa’s note to Justice Kamins, saying, “To the extent it energizes me to achieve a decisive victory it is a particular plus.”

By early summer, he seemed to realize his hold was slipping. He drafted a long letter to a member of the editorial board of The New York Times outlining what he had accomplished as district attorney, and sent it to Justice Kamins.

“This is compelling stuff — will go a long way to turning things around,” Justice Kamins wrote as he suggested some wording changes.

By July, he was leaning ever more heavily on staff members. On July 18, he emailed his deputy district attorney, Dino Amoroso. “Beginning next Thurs and for the remainder of the campaign I want you to attend the strategy meeting” for the campaign, he wrote. “Thx.”

Two days later, he forwarded a long message to Justice Kamins. “How does this look as an Opening Statement?” he asked.

Two days after that, he sent another email to a staff member. “Hi, Lance,” he wrote. “Any idea when the Carib news will decide to publish an endorsement in my race?”

The next day, he sent canvass numbers on a particular swing area — or, as Mr. Hynes put it, “White, Liberal, New York Times reading Brooklyn” — to the Brooklyn Conservative Party chairman. “Completely confidential,” Mr. Hynes wrote. “I think you’ll find the numbers very interesting.”

And on July 29, he sent a note to his campaign manager tallying what he could raise at upcoming fund-raisers, which he then forwarded to Mr. Amoroso. “The Paki event is being combined by Lance with some more Caribbean leaders and should yield $5K,” he wrote. “Overall that could net $40K plus what I can bring in today and Thurs.”

By August, he was going back and forth with top staff members in the district attorney’s office, along with Justice Kamins and political aides, refining talking points and drafting answers to debate questions.

He had heard that Representatives Jerrold Nadler and Nydia M. Velazquez were going to endorse Mr. Thompson by August. “If this is true I don’t see much impact. Since we are supported by a majority of Brooklyn’s state and city legislators, and by the County organization,” he wrote to a consultant. “What matters is I continue my ‘running scarred campaign’ and an aggressive GOTV on Primary Day,” referring to “get out the vote.”

Justice Kamins tried to reassure Mr. Hynes in August before a debate with Mr. Thompson: “Remember — you are the senior statesman and he is ... who he is.”

As anxious as he got, Mr. Hynes thought he would win until primary night, friends said.

A version of this article appears in print on , on Page A17 of the New York edition with the headline: In Brooklyn Prosecutor’s Emails, Last Grasps at Election. Order Reprints | Today’s Paper | Subscribe