Former congressman Anthony Weiner has pleaded guilty in federal court to transferring obscene material to a minor,Â according toÂ the U.S. Attorneyâs Office for the Southern District of New York.

Weiner, who appeared Friday in U.S. District Court in Manhattan, cried as he apologized to a 15-year-old girl with whom he had engaged inÂ sexually explicit conversations online, the Associated Press reported.

âI have a sickness, but I do not have an excuse,â Weiner, 52, said in court.Â âI apologize to everyone I have hurt. I apologize to the teenage girl, whom I mistreated so badly.â

Federal authoritiesÂ said that from January to March 2016, WeinerÂ communicated with the teen viaÂ online textÂ and video chat applications, sending herÂ obscene material and asking her to engage inÂ sexual conduct.

Weiner surrendered to FBI agents in New York early Friday and appeared in court late-morning, telling U.S. District Judge Loretta A. Preska how he hadÂ â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,âÂ according to a copy of his plea statement, which was later released by his attorney.

âI knew this was as morally wrong as it was unlawful,â he said.

Weiner, a Democrat who once represented a New York City congressional district,Â resigned from his House seat in 2011 after he inadvertently tweeted an explicit photo of himself that was meant for one of his supporters.Â Weiner said in court Friday that he has âcompulsively sought attentionâ from women who have reached out to him on social media and engaged with many of them in conversations.

âThis fall, I came to grips for the first time with the depths of my sickness,â he said, according to a copy of his statement. âI had hit bottom. I entered intensive treatment, found the courage to take a moral inventory of my defects, and began a program of recovery and mental health treatment that I continue to follow every day.â

Following Weinerâs plea, his attorney, Arlo Devlin-Brown,Â said his client hasÂ accepted âfull responsibility for the inappropriate, sexually explicit communications he engaged in early last year.â

âHe apologized, offered no excuses, and made a commitment to make amends,â heÂ said in a statement. âAs reflected in the plea agreement, the resolution on terms far less severe than could have been sought reflect âthe specific circumstances of the offense conduct in this case,â which did not feature aggravating factors often present in cases of this kind.Â Mr. Weiner will not be addressing this matter beyond his statement this morning in Court, and remains focused on his recovery.â

The details aboutÂ Weinerâs plea agreement are still unclear, but the Associated Press reported that he will be required to register as a sex offender.

The U.S. Attorneyâs Office said in a statementÂ the chargeÂ â transferring obscenity to a minorâ carries a maximum penalty of 10 years in prison.Â Weiner said he would not appeal a prison sentence between 21 and 27 months, according to the Associated Press.

Weiner was released on bail following his plea. His sentencing hearing is scheduled for Sept. 8.

â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 H. Kim said in a statement.

The criminal case came to the attention of the FBI last year when theÂ 15-year-old girl in North Carolina toldÂ the Daily MailÂ about her online communications withÂ Weiner.

During aÂ federal investigation, agentsÂ seized a laptop that Weiner had shared with his estranged wife, Huma Abedin, who was a top Hillary Clinton aide. On the laptop, agentsÂ discovered numerous emails that Clinton had sent toÂ Abedin, prompting an announcement from then-FBI Director James B. Comey in the days leading up to the presidential electionÂ that the agencyÂ was reopening its investigation into ClintonâsÂ emails.

ComeyÂ thenÂ announced thatÂ no charges would be brought in the case. But Clinton, and others, have said the scandal was partly the reason she lost the election to DonaldÂ Trump.