Get breaking news alerts and special reports. The news and stories that matter, delivered weekday mornings.

Disgraced former Congressman Anthony Weiner wept openly in federal court Friday as he admitted to sexting a 15-year-old girl — and broke down as he told the judge: "I have a sickness, but I do not have an excuse."

Weiner, 52, pleaded guilty in Federal District Court in lower Manhattan to one charge of transferring obscene material to a minor. Under a plea agreement, prosecutors asked he serve 21 to 27 months behind bars. He must also register as a sex offender.

Hours after his emotional court appearance, estranged wife Huma Abedin filed for divorce, a source close to the family told NBC New York. The couple has one child together.

During court, Weiner wore his wedding ring, according to The Associated Press. He answered the judge's questions before reading a prepared statement, explaining how he was contacted by the girl online in January 2016 and knew she was 15.

"Through approximately March 2016, I engaged in obscene communications with this teenager, including sharing explicit images and encouraging her to engage in sexually explicit conduct, just as I had done and continued to do with adult women," Weiner said. "I knew this was as morally wrong as it was unlawful."

Amid sobs, he said that last fall he "came to grips for the first time with the depths of my sickness. I had hit bottom," and chose to enter "intensive treatment." He then apologized to the girl.

Anthony Weiner appears in federal court and plead guilty to a charge of transmitting sexual material to a minor and could get years in prison. Courtesy Christine Cornell

Weiner, who was joined in court by his father and brother, was released on bail pending a Sept. 8 sentencing and did not answer reporters' questions as he left. A judge could decide instead to give Weiner probation. He faces up to 10 years in prison.

He also had to surrender his passport and iPhone, and agreed not to leave New York before his sentencing, according to NBC New York.

"Weiner’s conduct was not only reprehensible, but a federal crime, one for which he is now convicted and will be sentenced," acting Manhattan U.S. Attorney Joon Kim said in a statement.

Weiner, once considered a rising star in the Democratic Party, had represented his congressional district in Brooklyn and Queens for almost 12 years before a string of sexting scandals torpedoed his career. He stepped down in 2011 after admitting to sending sexually explicit social media messages and texts to women.

Let our news meet your inbox. The news and stories that matters, delivered weekday mornings. This site is protected by recaptcha

But the sexting didn't stop, and a series of explicit exchanges continued in early 2016 with the 15-year-old girl from North Carolina, two highly knowledgeable sources told NBC News in September.

Weiner allegedly sent pornographic videos and made requests that the girl be nude and masturbate, the sources said. He allegedly used the alias "T Dog" with her.

Former U.S. Rep. Anthony Weiner leaves Federal court on 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. Mary Altaffer / AP

Federal statutes "prohibit obscenity involving minors, and convicted offenders generally face harsher statutory penalties than if the offense involved only adults," according to the Justice Department.

DailyMail.com first published alleged screen grabs of texts sent by Weiner to the girl, whose identity was not disclosed.

Weiner in a statement to NBC New York later suggested that he was the victim of a hoax.

"While I have provided the Daily Mail with information showing that I have likely been the subject of a hoax, I have no one to blame but me for putting myself in this position," Weiner wrote at the time. "I am sorry."

He also provided the AP with an email that he says the girl wrote to a teacher supposedly recanting her claim.

The latest series of sexting allegations is a far cry from Weiner's congressional days, when he became a political star on C-SPAN and MSNBC — championing health care reform and other liberal causes.

In 2010, he gave a fiery speech from the House floor in support of a bill to aid 9/11 first responders, rebuking Republicans who sought to silence him. "I will not yield to the gentleman!" he screamed.

In recent years, after falling out of the headlines, Weiner continued to be the subject of political gossip surrounding his souring relationship with Abedin, who had been a close aide to Hillary Clinton for about two decades. A documentary called "Weiner," focusing on his sexting scandal, premiered last year in theaters.

Weiner and Abedin, who married in 2010, have been separated since last summer, when new accusations came to light that he sent more damning photos and messages. Eventually, reports surfaced that the messages were with the teenage girl.

A federal investigation into the case led FBI agents to seize Weiner's laptop. That prompted the feds in October to announce they had found a new batch of emails on the computer that "appear to be pertinent" to a previous investigation into Clinton's use of a private server while she was secretary of state.

In May, while FBI Director James Comey explained his decision to Senators for why he reopened the bureau’s investigation into Clinton’s emails, he testified that Abedin forwarded "hundreds and thousands of emails" to Weiner, including classified information.

But a week later, he acknowledged grossly overstating the number.

Weiner unsuccessfully ran for mayor of New York City in 2005 and then in 2013, after he hoped to put the initial texting scandal and his resignation from Congress behind him.

But a few months before the Democratic primary, a new round of sexting accusations came to light when it was revealed he used the alias "Carlos Danger" and admitted to sending messages to at least three women the year before. Shirtless and other explicit photos of him emerged online. One of the women identified herself as a 23-year-old from Indiana named Sydney Leathers.

Despite pressure to bow out of the race, Weiner — with Abedin by his side — famously vowed to fight on. But he finished fifth in a packed Democratic field, effectively putting the kibosh on a political second act.

Abedin was not in court Friday, when Weiner told the judge that for years he had "destructive impulses" that ruined his family life and crushed his dreams.

"And yet I remained in denial even as the world around me fell apart," he said.

"I am committed to making amends to all those I have harmed."