An NYPD detective is accused of cyberstalking his ex-girlfriend and hacking into the emails of other cops in his precinct because he thought she was cheating on him, NBC 4 New York has learned.

Law enforcement sources tell NBC 4 New York that the 42-year-old Bronx detective, Edwin Vargas, was arrested Tuesday by the FBI and NYPD Internal Affairs.

Sources say Internal Affairs first began questioning Vargas about a month ago about stalking his ex-girlfriend online, and then investigators learned he had allegedly been hacking into the email accounts of other cops.

In all, he allegedly tried to spy on more than 40 email accounts, including 21 with the NYPD, sources said. He is accused of hiring email hacking services to perform the break-ins, and is suspected of engaging in the illegal activity from 2010 to 2012, spending more than $4,000 for the information.

He is also accused of inappropriately accessing the National Crime Information Center, a database run by the FBI.

Vargas is charged in federal court with one count of conspiracy to commit computer hacking and one count of computer hacking.

"Of all places, the police department is not a workplace where one should have to be concerned about an unscrupulous fellow employee," FBI Assistant Director in Charge George Venizelos said in a statement.

Information on an attorney was not immediately available.

Vargas and the ex-girlfriend have a child together, sources said.