"Through my Title Ten Authority, I have striven to ensure our proceedings are fair, transparent and consistent, from the newest recruit to the Flag and General Officer level.
"Unfortunately it has become apparent that in this respect, I no longer share the same understanding with the Commander in Chief who appointed me, in regards to the key principle of good order and discipline.
"I cannot in good conscience obey an order that I believe violates the sacred oath I took in the presence of my family, my flag and my faith to support and defend the Constitution of the United States."
The letter goes on: "The President deserves and should expect a Secretary of the Navy who is aligned with his vision for the future of our force generation and sustainment."
The firing was a dramatic turn in a long-running controversy involving Navy Chief Petty Officer Edward Gallagher, whose case has been championed by Trump.
In a series of tweets, the President said he was "not pleased with the way that Navy Seal Eddie Gallagher's trial was handled by the Navy", saying he was "treated very badly".
Gallagher was convicted of bringing discredit to the armed services after posing next to a dead Islamic State fighter's body while deployed to Iraq in 2017. He was demoted before Trump intervened last week and ordered the navy to restore Gallagher's rank.
Esper also directed that Gallagher be allowed to retire at the end of this month and a navy disciplinary board that was scheduled to hear his case starting December 2 be cancelled, Hoffman said.
At Esper's direction, Gallagher would be allowed to retire as a SEAL at his current rank, Hoffman said.
Hoffman said Esper lost trust and confidence in Spencer "regarding his lack of candour" over conversations with the White House involving the handling of the SEAL case.
"I am deeply troubled by this conduct shown by a senior DOD official," Esper said in a written statement issued by Hoffman. "Unfortunately, as a result I have determined that Secretary Spencer no longer has my confidence to continue in his position. I wish Richard well."