The U.S. ambassador to Afghanistan, in unusually blunt and personal comments Sunday, responded sharply to President Hamid Karzai’s escalating denunciations of American and NATO forces and aid efforts in Afghanistan.

After an upbeat speech about Afghanistan’s future prospects to university students in the Western city of Herat, Ambassador Karl W. Eikenberry added remarks that he said were “spoken from my heart.” He complained bitterly about American forces being “compared to occupiers” and being “told that they are only here to advance their own interests,” suggesting such comments could lead the United States to give up on Afghanistan.

Although he did not mention Karzai by name, Eikenberry was referring almost verbatim to harsh criticisms the Afghan president voiced Saturday and has expressed on previous occasions. “I must tell you that I find occasional comments from some of your leaders hurtful and inappropriate,” Eikenberry said.

The ambassador’s remarks came as he prepares to leave Afghanistan at the end of a grueling two-year posting. His tenure coincided with the arrival of an additional 30,000 American troops last year — bringing the total to 100,000, along with more than 40,000 from other countries — and a much more aggressive U.S. strategy that has exacted a price in increasingly brittle relations with the Karzai government.

President Obama has pledged to bring the first of those surge troops home next month, and the White House is debating the pace of withdrawal. Although the Pentagon has argued for a slow drawdown, Obama is under increasing pressure — from the American public, Congress and even some inside the White House — to move more quickly. In addition to the expense of the war, lawmakers have cited the apparent ingratitude of the Afghan people, along with government corruption and incompetence.

Afghan leaders appear torn between a fear of losing U.S. military and economic assistance and resenting their dependence on it, and Karzai has become increasingly outspoken in criticizing U.S. actions.

Karzai has repeatedly denounced civilian casualties and night raids by U.S. and NATO forces. He has come close to demanding that they leave the country and threatened to label them foreign occupiers. In contrast, he has rarely criticized the Taliban insurgents, despite their brutal campaign against his government and populace, instead inviting them to join the government as “sons of the soil.”

Last month, Karzai threatened to attack foreign troops if they did not stop airstrikes that kill civilians, saying that continued strikes would turn the forces from defenders to enemies of the Afghan people. “In that case,” he said, “history shows what Afghans do with trespassers and with occupiers.”

In cables to Washington that were subsequently leaked, Eikenberry has privately criticized Karzai as unreliable and erratic. Until now, however, he has publicly hewed to Washington’s policy of tolerating Karzai’s increasingly emotional and anti-Western rants, in part because he is the nation’s elected leader and in part because Afghanistan’s cooperation is crucial to U.S. efforts to build stable and friendly allies in the volatile, terrorism-plagued region.

On Saturday, however, Karzai appeared to have crossed an unspoken line when, in a rambling speech to a youth convention in Kabul, he accused the United States and other Western allies of using his country for their own purposes. He asserted that they take away more money than they give, pollute Afghanistan’s environment and “dishonor” the Afghan people.

In his response Sunday, Eikenberry said, “When Americans, who are serving in your country at great cost — in terms of lives and treasure — hear themselves compared with occupiers, told that they are only here to advance their own interest, and likened to the brutal enemies of the Afghan people . . . they are filled with confusion and grow weary of our effort here.”

“We begin to lose our inspiration to carry on,” he added.

The outgoing envoy suggested that his impending departure made him feel such insults more deeply, but he also seemed to be calling Karzai’s bluff. His comments, quickly distributed to the news media by the U.S. Embassy, clearly reflected the deepening disillusionment about Karzai and his government that is shared by many in Washington as the Obama administration prepares to reduce the number of troops and begin a transition from Western to Afghan control of security and governance.

During a question-and-answer session after his speech, Eikenberry warned that “should we lose our mutual confidence in each other, should we lose our mutual respect, those goals of transition could be compromised.”

Eikenberry’s comments came as Defense Secretary Robert M. Gates, speaking Sunday on a taped CNN program, said that only continued military pressure would bring Taliban insurgents to the negotiating table and that he did not expect negotiations to make serious headway until winter. Taliban leaders have said they will refuse to negotiate until all foreign troops leave Afghanistan.

Gates was responding to Karzai’s comments Saturday that the United States is involved in peace talks already. Gates said there had been “outreach” by the United States and others to the Taliban but that the contacts were “very preliminary at this point.”

Eikenberry, who served in Afghanistan a total of five years as a senior U.S. Army general and then as ambassador, seemed intent on setting the U.S. record here straight.

“America has never sought to occupy any nation in the world,” he said. “We are a good people.. . . We came here in 2001 to defeat international terrorism and help lift the dark veil of over 20 years of conflict.” He ticked off a long list of U.S. efforts in Afghanistan over the past decade: building schools and roads, training the national army and police, improving agriculture, and restoring historic sites.

But he warned that if Afghan leaders reach the point where they “believe that we are doing more harm than good,” then Americans may “reach a point that we feel our soldiers and civilians are being asked to sacrifice without a just cause,” and “the American people will ask for our forces to come home.”

Staff writer Karen DeYoung in Washington contributed to this report.