WASHINGTON (Reuters) - The U.S. Supreme Court on Monday agreed to resolve a major privacy dispute between the Justice Department and Microsoft Corp <MSFT.O> over whether prosecutors should get access to emails stored on company servers overseas.

The justices will hear the Trump administration's appeal of a lower court's ruling last year preventing federal prosecutors from obtaining emails stored in Microsoft computer servers in Dublin, Ireland in a drug trafficking investigation.

That decision by the New York-based 2nd U.S. Court of Appeals marked a victory for privacy advocates and technology companies that increasingly offer cloud computing services in which data is stored remotely. Prosecutors say a ruling in favor of Microsoft could undermine a range of criminal investigations.

Microsoft, which has 100 data centers in 40 countries, was the first U.S. company to challenge a domestic search warrant seeking data held outside the country. There have been several similar challenges, most brought by Google.<GOOGL.O>

"If U.S. law enforcement can obtain the emails of foreigners stored outside the United States, what's to stop the government of another country from getting your emails even though they are located in the United States?" Brad Smith, Microsoft's president and chief legal officer, said in a blog post on Monday.

A Justice Department spokesman declined to comment.

Government lawyers said in court papers that the lower court ruling "gravely threatens public safety and national security" because it limits the government's ability to "ward off terrorism and similar national security threats and to investigate and prosecute crimes."

The case attracted significant attention from technology and media companies concerned that a ruling favoring the government could jeopardize the privacy of customers and make them less likely to use cloud services because of concern that data could be seized.

Though Microsoft is based in Washington state, the court said the emails were beyond the reach of U.S. domestic search warrants issued under a 1986 law called the Stored Communications Act. The Microsoft customer in question told the company he was based in Ireland when he signed up for his account.

Bipartisan legislation has been introduced in Congress to update the 1986 law, a move that Microsoft has backed.

"Regardless of the ultimate decision, Congress should resolve the issue of cross-border law enforcement data demands, with a clear focus on strong human rights protections for all internet users," said Greg Nojeim, a senior counsel at the Center for Democracy & Technology digital rights group, which backs Microsoft in the case.

The Microsoft dispute is the second that the justices have agreed to hear in their current term that touches upon privacy rights in the digital age. The other case concerns whether police officers need a warrant to access information on the past locations of cell phone users that is held by wireless carriers.

Rulings in both cases are due by the end of June.

BEYOND THE REACH

The lower court ruling put stored electronic communications held overseas beyond the reach of U.S. prosecutors even when there is probable cause that they contain evidence of a crime, Justice Department lawyers said in court papers.

In the appeals court, Microsoft was supported by dozens of technology and media companies including Amazon, Apple, CNN and Verizon Communications, as well as the American Civil Liberties Union and the U.S. Chamber of Commerce business group.

A coalition of 33 U.S. states and Puerto Rico backed the Justice Department's appeal, saying in court papers that obtaining customer data from tech companies is vital to a broad range of criminal investigations, including child sexual exploitation and murder.

In the underlying case, the government in 2013 sought a warrant requiring information about a Microsoft email account that prosecutors believed was being used for narcotics trafficking. No further details about the investigation have been made public.

The Supreme Court has twice in recent years ruled on major cases concerning how criminal law applies to new technology, on each occasion ruling against law enforcement. In 2012, the court held that a warrant is required to place a GPS tracking device on a vehicle. In 2014, the court ruled that police need a warrant to search a cellphone seized during an arrest.