(Reuters) - United Parcel Service Inc has agreed to forfeit $40 million it earned from illegal Internet pharmacies shipping drugs using its services, U.S. authorities said Friday.

As part of the settlement, UPS entered a non-prosecution agreement with the U.S. Department of Justice.

The company also agreed to put a compliance program into place to prevent illegal online pharmacies from distributing drugs through its shipping services in the future, authorities said.

"Good corporate citizens like UPS play an important role in halting the flow of illegal drugs that degrade our nation's communities," Northern California U.S. Attorney Melinda Haag said in a statement.

UPS cooperated with the investigation, prosecutors said.

Prosecutors said UPS was on notice from 2003 to 2010 that Internet pharmacies were shipping drugs without prescriptions, yet the company didn't put procedures in place to shut down their accounts.

"We believe we have an obligation and responsibility to help curb the sale and shipment of drugs sold through illegal Internet pharmacies," said Susan Rosenberg, a UPS spokeswoman.

(Reporting by Nate Raymond in New York; Editing by Chizu Nomiyama)