Energy firm Npower is to pay £26m after it sent out inaccurate bills and failed to deal with complaints correctly, Ofgem has said.
The energy watchdog said Npower was to pay the record settlement "for failing to treat customers fairly".
Npower's billing issues affected more than 500,000 customers between September 2013 and December 2014.
Ofgem said the £26m would be divided between some of the worst affected customers and charity.
The settlement is the largest yet agreed between Ofgem and a "big six" energy supplier.
Npower was fined £2m in October 2011 for a similar offence.
Ofgem said that many of Npower's problems occurred after a new IT system was introduced in 2011.
Between September 2013 and December 2014, the company issued more than 500,000 late bills, while some customers also received inaccurate bills.
The regulator said that during this period, more than two million complaints were made by Npower customers.
However, Ofgem found that "Npower often failed to resolve these issues promptly, pursued debts which were in dispute and failed to keep its own commitments to customers on billing. This caused significant distress and worry for many."
The worst affected customers did not receive bills for more than a year. Under UK billing rules, companies cannot charge for energy used more than a year earlier if previously unbilled, but Npower did.
Failures in the IT system led to late and inaccurate bills, but also inefficient complaint handling, Ofgem said. For several years, complaints were recorded incorrectly, leading to unresolved issues being recorded as resolved, and multiple records created for one complaint.
Dermot Nolan, Ofgem chief executive, said: "Npower failed its customers. Not only have its billing and complaint handling procedures been chaotic, it treated many of its customers poorly, which is completely unacceptable.
"Npower's management failed to act quickly enough to protect its customers when things went wrong with changes to its IT system."
The regulator said it had agreed targets with Npower for it to improve its billing and complaints handling.
If Npower fails to meet the targets, the company will have to stop all UK sales and advertising until they are met.
Simon Stacey, managing director of domestic markets for Npower, said: "The last few years, since we changed the systems that support our domestic business, have been very disappointing for our customers - and for Npower.
"We are very sorry about what has happened and that is why we have agreed this significant package of customer redress."
He said the company had reduced complaints by almost 70% since the beginning of the year.