The stolen data was used to gather personal information about people those Yahoo mail users have recently corresponded with.
That could mean hackers were looking for additional email addresses to send spam or scam messages. By grabbing real names from those sent folders, hackers could try to make bogus messages appear more legitimate to recipients.
Yahoo did not say how many accounts have been affected. Yahoo is the second-largest email service worldwide, after Google's Gmail, according to the research firm comScore. There are 273 million Yahoo mail accounts worldwide, including 81 million in the U.S.
Experts say the breach may lead to an even bigger danger. Access to email accounts could lead to more serious breaches involving banking and shopping sites. That's because many people reuse passwords across many sites, and also because many sites use email to reset passwords.
For instance, hackers could try logging in to such a site with the Yahoo email address and ask that a password reminder be sent by email.
Yahoo said the usernames and passwords weren't collected from its own systems, but from a third-party database. The company said passwords on hacked accounts are being reset and that additional measures are being taken to block further attacks. Yahoo also said it is working with the federal government.
The breach is the second mishap for Yahoo's mail service in two months. In December, the service suffered a multi-day outage that prompted Yahoo CEO Marissa Mayer to issue an apology.
This is the latest in a string of security breaches that have allowed hackers to nab personal information using software that analysts say is ever more sophisticated. Up to 70 million customers of Target stores had their personal information and credit and debit card numbers compromised late last year, and Neiman Marcus was the victim of a similar breach in December.
The Associated Press contributed to this report.