Defense Secretary James Mattis will resign at the end of February, he informed President Trump in a letter Thursday. The defense secretary said he is stepping down because of the differences in viewpoints with Mr. Trump.

"Because you have the right to have a Secretary of Defense whose views are better aligned with yours on these and other subjects, I believe it is right for me to step down from my position," Mattis wrote.

Mr. Trump announced the news in a tweet and praised Mattis' tenure as secretary.

Get Breaking News Delivered to Your Inbox

"During Jim's tenure, tremendous progress has been made, especially with respect to the purchase of new fighting equipment," Mr. Trump wrote on Twitter. "General Mattis was a great help to me in getting allies and other countries to pay their share of military obligations. A new Secretary of Defense will be named shortly. I greatly thank Jim for his service!"

News of Mattis' impending departure comes the day after Mr. Trump's sudden announcement about Syria, a decision he made without consulting security officials in his administration.

A senior White House official said that Mattis came to the White House for a meeting, not previously scheduled, around 3:30 p.m. Thursday, and informed the president he would be leaving. The president hopes to announce a replacement by the end of the year.

Read Mattis' resignation letter here:

Mattis also appeared to be subtly criticizing Mr. Trump's "America first" foreign policy in his resignation letter, discussing the importance of maintaining alliances.

"One core belief that I have always held is that our strength as a nation is inextricably linked to our unique and comprehensive system of alliances and partnerships," Mattis wrote. He also warned against closeness with countries such as Russia and China.

"My views on treating allies with respect and also being clear-eyed about both malign actors and strategic competitors are strongly held and informed by over four decades of immersion in these issues," he added, perhaps a rebuke to Mr. Trump's lack of foreign policy expertise.

Both Republican and Democratic lawmakers expressed regret that Mattis was leaving the administration. House Democratic Leader Nancy Pelosi said she was "shaken" by Mattis' resignation and expressed admiration for the letter he wrote to the president. "I think that everybody in the country should read his letter of resignation," she told reporters. "It's a letter of great patriotism, respect for the President, but also a statement of his values."

White House Press Secretary Sarah Sanders told reporters Thursday that Mattis and Mr. Trump had a good working relationship, playing down the two's policy differences. Sanders said that Mr. Trump did not ask Mattis to resign.

Mattis was considered to be one of the few cabinet officials to whom Mr. Trump would listen, although his influence over the president had waned in recent month. Mattis, along with departing chief of staff John Kelly, was one of the military men tapped for an administration position by Mr. Trump, who fondly referred to them as "my generals."

Mattis is the latest of these former generals to leave the administration, along with Kelly and former national security adviser H.R. McMaster. Mattis was reported to have agreed to a "suicide pact" with former Secretary of State Rex Tillerson and Treasury Secretary Steve Mnuchin last year, in which the three agreed to leave the administration if one was forced out.

Mr. Trump's first two years in office have seen significant upheaval in his cabinet. The Trump administration has had three chiefs of staff, three national security advisers, two secretaries of state, two Homeland Security secretaries, two health and human services secretaries and two VA secretaries. There are currently vacancies for the posts of attorney general, U.N. ambassador, interior secretary and defense secretary.

Mr. Trump's tweet came amid negotiations to pass a partial government funding bill.