The letter was delivered as Harper formally designated Sept. 11 a national day of service to commemorate the attacks.
Obama said Canada came through when it counted after the World Trade Center was taken down by hijacked airliners.
"In one of the darkest moments of our history, Canada stood by our side and showed itself to be a true friend," he wrote.
He paid a special tribute to Gander, N.L, which took in 6,700 airline passengers whose flights were diverted when North American air space was closed.
"We remember with gratitude and affection how the people of Canada offered us the comfort of friendship and extraordinary assistance that day and in the following days by opening their airports, homes and hearts to us."
The president also thanked Canada for its solidarity in fight terrorism today.
"On this anniversary, we recognize all the gestures of friendship and solidarity shown to us by Canada and its people and give thanks for our continuing special relationship."
In his own letter in reply, the prime minister said he and Canada extend "our sincere expression of solace and remembrance."
"Ten years later, we pay tribute to the victims of the September 11 terrorist attacks and honour the survivors and families whose lives were forever altered that day."
Harper said the national day of service will pay tribute to the victims and to the Canadians and communities who took in stranded travellers when air transport shut down in the wake of the attacks.
The prime minister, who travels to New York this weekend to take part in commemoration ceremonies, said 9-11 was not only about death and destruction.
"It is equally important to recall the incredible acts of courage, sacrifice and kindness by Canadians on and following that infamous day," he said.
Following the attacks, dozens of airliners were forced to land at the nearest airport. Gander, a community of 10,000, has drawn high praise from many Americans for its generosity. Stranded travellers were billeted in homes and schools, fed by local businesses and families and supplied with clothes, medicines and other needs.
Harper said that's a memory to be cherished.
"I hope that this national day of service, observed hereafter on Sept. 11, will inspire Canadians to once more show the same kind of compassion to strangers in need, by engaging on that day in charitable activities, fundraisers and community service for worthy causes across the country."
On Sunday, Harper will join a public memorial service in Lower Manhattan, site of the former World Trade Center buildings whose destruction 10 years ago also tore through Western society.
"On this day, we will pay tribute to Canadians, Americans and all those who lost their lives nearly 10 years ago in these heinous attacks," Harper said in a statement.
"As we pay tribute to the victims and their loved ones, we also honour members of the military, law enforcement and intelligence personnel who continue to fight on the front lines against all forms of terrorism."
The evening before, the prime minister will meet privately with the families of Canadians killed in the co-ordinated terrorist attacks in Washington and New York. Harper will show his support for those affected by "the senseless and tragic attacks," said a statement issued by the prime minister's office.
"It is a fitting way to pay tribute to the Canadians and others who were lost in 9-11, to show continued support for the families of victims, to honour the sacrifices made by those who served in the rescue efforts, and to turn an infamous date into a day of hope marked by a communal outpouring of warmth and generosity."
Harper also ordered the flag on the Peace Tower to be lowered to half mast on Sunday.