Christopher Mawhinney pestered his victim with phone calls and threw stones at windows at her home in a Dumfries and Galloway town before carrying out the sex assault on her.
A judge told Mawhinney (25) at the High Court in Edinburgh as he jailed him: "There is no alternative to a custodial sentence."
Lord Braid added: "You were convicted by the jury of raping and sexually assaulting the complainer."
Mawhinney, formerly of Kennels Cottage, Mossdale, turned up at the address in the early hours of Friday, December 27, in 2019 and persisted in his advances on the woman, who was aged 24 at the time.
During the sex attack on the unwilling victim Mawhinney molested her.
Lord Braid revealed Mawhinney had been assessed in a background report prepared on him as posing a medium risk of sexual offending.
The judge said: "It is said your intention in going to the house that night was to engage in sexual activity."
Lord Braid said that he took into account Mawhinney's relatively young age at the time of the offence, which was committed when he was 23.
Mawhinney, who has a previous conviction for assault, had denied raping the woman at his earlier trial and the court heard that he continued to maintain his denial of the offence.
He was also convicted of a stalking offence between December 25 and 27 in 2019 by engaging in conduct that caused fear and alarm to the victim by repeatedly phoning her, attending at her address, throwing stones at windows and repeatedly demanding entry.
Defence counsel Niall McCluskey said: "He clearly maintains his position that he put forward at trial."
He said Mawhinney had "a good work ethic" and a pro-social peer group who appeared to be supportive of him.
The defence counsel said: "The long and the short of it is that he accepts there will be a custodial sentence."
Lord Braid told Mawhinney he would be on the sex offenders' register for an indeterminate period.