McGrath, 60, of Grantchester Street, Cambridge, pleaded guilty at Huntingdon Magistrates' Court, in Cambridgeshire.
He had been charged with following the woman, sending electronic messages and approaching her in the street.
McGrath was sentenced to 10 weeks in jail suspended for 18 months.
He was also given a five-year restraining order and ordered to pay Â£200 in costs.
McGrath originally denied a stalking charge but admitted harassment when the charge was changed.
The offences happened in Cambridge over a 14-month period between June 2015 and August last year.
More on this and other news from Cambridgeshire
Prosecutors said McGrath sent letters to the woman's husband.
The court heard his victim, who cannot be named for legal reasons, had a physical relationship with McGrath between November 2010 and May 2015.
"Both the defendant and the complainant were married to other people and their relationship had managed to be kept secret from their respective spouses," Anthony Abell, prosecuting, told magistrates.
By August 2014 the victim had begun to "find Mr McGrath difficult and unpleasant company" and wanted to end the affair, he said.
Mr Abell said: "She made it clear to him that she wanted it to end towards the end of May."
At this time she asked McGrath to delete intimate photos of them together, which he had on his phone and other devices, the court heard.
Mr Abell said McGrath "seems to have found it very difficult to deal with" and became "unstable and tempestuous".
He said the pair met a few times afterwards, but in July the victim "made it clear to him the relationship was over".
In a statement read on his behalf by his solicitor Nick Barnes, McGrath said: "This has been a dark time and thankfully it's now over.
"I wish to thank the judge and to apologise to my wife and family and to thank them for their incredible support during this time. I now want to move on with my life. Thank you."
The TV star has appeared in programmes including BBC panel show They Think It's All Over and comedy documentary Three Men In A Boat.
Television personality Rory McGrath has been given a suspended prison sentence after admitting harassing a married woman for more than a year after she ended their affair.