Former U.S. Rep. Anthony Weiner leaves Federal court, Friday, May 19, 2017, in New York. Weiner pleaded guilty to a charge of transmitting sexual material to a minor and could get years in prison. (AP Photo/Mary Altaffer)

Former U.S. Rep. Anthony Weiner leaves Federal court, Friday, May 19, 2017, in New York. Weiner pleaded guilty to a charge of transmitting sexual material to a minor and could get years in prison. (AP Photo/Mary Altaffer)

Former U.S. Rep. Anthony Weiner leaves Federal court, Friday, May 19, 2017, in New York. Weiner pleaded guilty to a charge of transmitting sexual material to a minor and could get years in prison. (AP Photo/Mary Altaffer)

In this courtroom artist's sketch, Anthony Weiner, right, accompanied by his attorney Arlo Devlin-Brown, reads a statement during a hearing, Friday, May 14, 2017 in New York Federal court. Weiner pleaded guilty to transmitting sexual material to a minor. (Jane Rosenberg via AP)

Former U.S. Rep. Anthony Weiner leaves Federal court, Friday, May 19, 2017, in New York. Weiner pleaded guilty to a charge of transmitting sexual material to a minor and could get years in prison. (AP Photo/Mary Altaffer)

Former U.S. Rep. Anthony Weiner leaves Federal court, Friday, May 19, 2017, in New York. Weiner pleaded guilty to a charge of transmitting sexual material to a minor and could get years in prison. (AP Photo/Mary Altaffer)

Former U.S. Rep. Anthony Weiner leaves Federal court, Friday, May 19, 2017, in New York. Weiner pleaded guilty to a charge of transmitting sexual material to a minor and could get years in prison. (AP Photo/Mary Altaffer)

NEW YORK (AP) — Former U.S. Rep. Anthony Weiner, whose penchant for sexting strangers ended his political career and sparked a probe that upended the presidential race, pleaded guilty Friday to a sex charge, tearfully apologizing for communications with a 15-year-old girl that he said destroyed his "life's dream in public service."

Weiner, who could go to prison, pleaded guilty to a single count of transmitting obscene material to a minor. He admitted exchanging online messages with the girl beginning in January 2015 and "sharing explicit images and encouraging her to engage in sexually explicit conduct."

"I have a sickness, but I do not have an excuse," he said.

In court, the 52-year-old former Democratic congressman paused repeatedly as he fought back tears and tried to compose himself. He said he knew the texting was "as morally wrong as it was unlawful."

Pleading to the charge, which requires him to register as a sex offender, could bring a sentence of up to 10 years. But Weiner is likely to serve much less time if he is sentenced to prison.

In a plea agreement with prosecutors, he agreed not to appeal any sentence between 21 and 27 months in prison. His lawyer can request leniency at a sentencing scheduled for Sept. 8.

In a written plea agreement, prosecutors said aggravating factors such as the age of the victim would have called for a prison sentence of up to 14 years under sentencing guidelines were it not for the plea bargain and a 10-year maximum penalty on the charge.

Wearing his wedding ring and a dark blue suit with a maroon tie, Weiner read from a prepared statement after U.S. District Judge Loretta A. Preska addressed him: "Tell me what you did, sir."

He said he "compulsively sought attention from women who contacted me on social media" beginning with his service in Congress and continuing through the first half of last year.

"I engaged with many of them in both sexual and nonsexual conversation," he said. "These destructive impulses brought great devastation to my family and friends and destroyed my life's dream in public service. Yet I remained in denial even as the world around me fell apart."

Weiner said he began getting mental health treatment in the fall, when he said he "came to grips for the first time with the depths of my sickness." He said he continues to follow the treatment daily.

"I had hit bottom," he said. "Through treatment I found the courage to take a moral inventory of my defects."

Weiner apologized to "everyone I have hurt," including the girl he "mistreated so badly."

Finished speaking, he wiped his eyes with tissues.

Weiner was already in federal custody ahead of the hearing, which lasted less than half an hour. Afterward, he shook hands with prosecutors, telling them: "Thanks, guys. I appreciate your service."

He then went down the courthouse elevator surrounded by his lawyers and court officers and left the building. He said nothing to reporters. His wife, Huma Abedin, was not in court.

Abedin, a top aide to Democratic presidential candidate Hillary Clinton last year, separated from Weiner in September after revelations he had sent more sexually charged messages online.

That same month, the FBI began investigating Weiner after the North Carolina girl told the news website DailyMail.com that she and Weiner had exchanged lewd messages for several months.

She also accused him of asking her to undress on camera. Phone messages left Friday with the North Carolina girl's father were not immediately returned.

As part of his plea, Weiner agreed to forfeit his iPhone.

In a statement, attorney Arlo Devlin-Brown said his client had "apologized, offered no excuses and made a commitment to make amends." He said the plea agreement reflected a resolution "on terms far less severe than could have been sought" because the circumstances lacked the kind of aggravating factors often present in similar cases. He added that Weiner remains "focused on his recovery."

In a release, acting Manhattan U.S. Attorney Joon Kim called Weiner's conduct "reprehensible," saying he sent sexually explicit images and directions to engage in sexual conduct to a girl he knew was 15.

The investigation led FBI agents to seize his laptop computer, which led to the discovery of a new cache of emails that Clinton had sent to Abedin.

In late October, just days before the election, FBI Director James Comey stunned the country by announcing that his agency was reopening its closed investigation into Clinton's handling of State Department business on a private email server so it could analyze the newly discovered correspondence.

That inquiry was brief. Comey announced shortly before the election that the new emails contained nothing to change his view that Clinton could not be charged with a crime. But Clinton partly blamed her loss to Republican Donald Trump on Comey's announcement.

Weiner, who lives in Manhattan and represented New York in Congress from 1999 to 2011, resigned after revelations that he was sending sexually explicit messages to multiple women.

He ran for New York City mayor in 2013 and was leading several polls until it was revealed he had continued his questionable behavior. His failed mayoral bid was the subject of the documentary "Weiner."