WASHINGTON — President Donald Trump heaped praise on law enforcement while decrying anti-police sentiment in a speech to FBI academy graduates on Friday not long after he lamented the agency's "sad" and "disgraceful" state.

In remarks to the FBI National Academy that also touched on immigration and violent crime, Trump called himself a "true friend and loyal champion" of police while noting that members of law enforcement "rarely get the recognition" they deserve.

"We will protect those who protect us," the president said, adding that those accused of killing police officers "should get the death penalty."

"Anti-police sentiment is wrong and it's dangerous, and we will not stand for it," he said.

An hour earlier, speaking to reporters on the White House lawn on his way to talk to graduates of the academy in Quantico, Virginia, Trump said: "It's a shame what's happened with the FBI, but we’re going to rebuild the FBI. It’ll be bigger and better than ever."

Referencing the 90 pages of newly released messages, many critical of the president, between an FBI lawyer and an agent later assigned to special counsel Robert Mueller's Russia probe, Trump called it "sad when you look at those documents and how they've done that is really, really disgraceful and you have a lot of very angry people that are seeing it."

In his speech, the president also made a pitch for cracking down on immigration and stepped up his attacks on the visa lottery system and chain migration, which his administration has called on Congress to end in the wake of recent terror attacks in New York City.

He also painted an inaccurate picture, however, of a system that invites the "worst people," insinuating that names were put "in a bin" and chosen out at random. (Actually, visa lottery applicants must meet eligibility requirements to enter the program, and applicants are vetted through strict State Department processes.)

"Congratulations, you're going to the United States!" Trump said. "What a system."

Promises of an immigration crackdown spurred applause from the law enforcement crowd as Trump went on to issue a new message to members of the MS13 gang Friday. "We will find you, we will arrest you, we will jail you, we will throw you the hell out of the country," he said.

But the last option was the one preferred by the president, he said, because in jail "we have to take care of them — who the hell wants to take care of them?"

The "jail stuff," Trump said over laughter from the crowd, "is wonderful, but we have to pay for them right?"

The FBI’s website describes the National Academy as a 10-week “professional course of study for U.S. and international law enforcement managers nominated by their agency heads because of demonstrated leadership qualities.”

Trump tweeted this month that the FBI's "reputation is in tatters," prompting FBI staffers — including Trump's own pick to head the agency after he fired former director James Comey — to defend it against the president's assertions.

After years of Comey, with the phony and dishonest Clinton investigation (and more), running the FBI, its reputation is in Tatters - worst in History! But fear not, we will bring it back to greatness. — Donald J. Trump (@realDonaldTrump) December 3, 2017

"The FBI that I see is tens of thousands of agents and analysts and staff working their tails off to keep Americans safe from the next terrorist attack, gang violence, child predators, spies from Russia, China, North Korea and Iran," FBI director Christopher Wray said last week during an appearance before the House Judiciary Committee.

During a passionate, two-minute-long defense, Wray described the FBI as "respected and appreciated by our partners in federal, state, and local law enforcement, in the intelligence community, and by our foreign counterparts in both law enforcement and national security in something like 200 countries around the globe."

Later on Friday, in response to questions from reporters, Attorney General Jeff Sessions said he did not agree with Trump's dim assessment of the FBI.

"Well, I don't share the view that the FBI is not functioning at a high level," Sessions said during a press conference announcing new anti-violent crime initiatives. "In my view, the FBI has huge national security requirements, it’s also fulfilling a fabulously important role in helping fight against violent crime, also."