Mattis who was seen as a steady hand and a leader who valued traditional relationships with foreign powers originally enlisted in the Marine Corps at the age of 18.
The brief farewell note opened by citing a one sentence telegram sent by President Abraham Lincoln to General Ulysses S. Grant that read "Let nothing which is transpiring change, hinder or delay your military movements, or plans".
US Defense Secretary Jim Mattis bade farewell to the Pentagon on Monday, telling the US military to "hold fast", after he quit over a series of fundamental differences with President Donald Trump. Trump himself seemed taken aback, and three days later, he tweeted that Shanahan would replace Mattis on January 1, cutting short Mattis' announced plan to stay until February 28 to ensure a smooth transition.
Mattis final letter also referenced US alliances - a key feature of his earlier letter.
In his message released Monday, his last day on the job, Mattis urged employees to "keep the faith" in America and work alongside USA allies against adversaries. I am confident that each of you remains undistracted from our sworn mission to support and defend the Constitution while protecting our way of life.
Mattis announced his departure after Trump declared victory over ISIS.
Kim Jong Un's fireside chat shows North Korea bid for acceptance
Pyongyang conducted its latest missile launch in November previous year . For either of these to happen, sanctions would have to be removed.
He also said that the Defense Department is at its best when "times are most difficult" - a surprising remark, given that now the military is not particularly burdened, at least not as much as it was in recent memory during the invasions of Afghanistan an Iraq.
The US president pushed James Mattis out of his post after disagreement over Trump's decision to end the US military presence in Syria and scale it back in Afghanistan, making Trump further isolated at helm. It ended with, "you have a right to have a Secretary of Defense whose views are better aligned with yours".
"My views on treating allies with respect and being clear-eyed about both malign actors and strategic competitors are strongly held and informed by over four decades of immersion in these issues", Mattis wrote in the letter.
Trump picked Shanahan, a former Boeing Co. executive with no previous government or military experience, to serve as acting defence secretary, pending a decision on who would be nominated to serve in a more permanent capacity.
Mattis is scheduled to transfer responsibility to his deputy, Patrick Shanahan, on Monday evening.
Via Twitter, Trump has suggested that Shanahan's service as Acting Secretary could be lengthy but he has also said there is no lack of candidates for the job.