CIA director David Petraeus's extramarital affair, which forced him to resign, was uncovered during an investigation into a potential breach of his Gmail account, reports say.
The check revealed Mr Petraeus's affair with his biographer, Paula Broadwell.
The FBI was investigating whether Mrs Broadwell had access to Mr Petraeus's account, after receiving a complaint that she had harassed another woman.
Mr Petraeus described his behaviour as "unacceptable" for the CIA's leader.
President Barack Obama, re-elected just three days before the resignation, praised Mr Petraeus for his "extraordinary service".
The four-star general became CIA boss in 2011 after heading international forces in Iraq and later in Afghanistan.
He was the highest-profile military officer of the post-9/11 years, winning plaudits for his role running the "surge" in Iraq and implementing a counter-insurgency strategy in Afghanistan.
He left his command role in Afghanistan in mid-2011, resigning from the US Army to head the CIA after Leon Panetta became defence secretary.
'Extremely poor judgment'
Shortly after Mr Petraeus resigned, reports emerged that the FBI had uncovered the affair during the course of an investigation into a potential breach involving his emails.
US officials said the FBI had begun an inquiry into Mrs Broadwell several months ago, after a complaint that she had sent harassing emails to another woman.
The identity of the second woman has not been revealed.
The New York Times quoted officials as saying she was not a family member, nor in government.
The paper also said Mrs Broadwell was found to be in possession of classified information, although it appeared Mr Petraeus had not passed it to her.
FBI agents checked whether Mrs Broadwell had access to Mr Petraeus's email account, then met him to discuss the investigation.
Announcing his decision to stand down, the former general was full of contrition.
"After being married for over 37 years, I showed extremely poor judgment by engaging in an extramarital affair," Mr Petraeus said in a statement.
"Such behaviour is unacceptable, both as a husband and as the leader of an organisation such as ours. This afternoon, the president graciously accepted my resignation."
Mrs Broadwell is a Harvard University research associate and PhD candidate at King's College, London.
She has a military background, graduating from the same West Point Academy as Mr Petraeus. She is married to radiologist Scott Broadwell and lives in Charlotte, North Carolina.
She spent months alongside the then-general in Afghanistan while researching her 2011 book, All In: The Education of General David Petraeus. The book was widely seen as a positive account of his leadership methods.
Last week Mrs Broadwell wrote a piece for Newsweek entitled General David Petraeus's Rules for Living.
In his statement, Mr Obama said Mr Petraeus had "provided extraordinary service to the United States for decades", citing both his time as CIA director and as a top general.
"By any measure, through his lifetime of service David Petraeus has made our country safer and stronger."
"Going forward, my thoughts and prayers are with Dave and Holly Petraeus, who has done so much to help military families through her own work."
Senior Obama adviser David Axelrod said the president had not known about the Petraeus issue during the campaign, and that the former general had come to Mr Obama after the election to offer his resignation.
CIA deputy director Michael Morell will serve as acting director of the agency, the White House confirmed. Eventually Mr Obama must nominate a new director to head the agency, who will then need to be confirmed by the Senate.
Mr Morell, who is well respected at both the White House and on Capitol Hill, also served as acting director following the departure of Mr Panetta.
The CIA faces a potential period of instability after Mr Petraeus' resignation, as it deals with a budget squeeze.