Farrukh Khan, 36, dragged the victim from a toilet at a flat before subjecting her to the sex ordeal.
A judge told Khan at the High Court in Edinburgh that he took into account that he was a first offender.
However, the Lord Justice Clerk, Lord Carloway, added that the court must take "a serious view" of such offending.
Khan had denied raping the 32-year-old woman on June 27, 2011, but was convicted of the offence by a jury.
Khan was previously placed on the sex offenders' register.