Sheffield Crown Court was told how Roman Rocha Lawrence's offending came to light when a package of drugs he had ordered was intercepted at an international Royal Mail sorting office in Berkshire earlier this year.
Michael Tooley, prosecuting, told the court: "The package was found to contain 109 tablets of MDMA, commonly known as ecstasy. The total weight of the controlled drug was 4.2 grams."
The discovery of the package led police officers to conduct a search at Rocha Lawrence's address, who is in the third year of a bio-engineering degree at the University of Sheffield.
Mr Tooley continued: "Various mobile phones were found and all were examined. The mobile telephones were found to contain messages consistent with the supply of Class A drugs, and to one user in particular.
"Also found in the defendant's home were smaller amounts of other controlled drugs of class B or C."
He added: "He [Rocha Lawrence] accepted his part in the importation of the controlled drug. He said he was going to use some himself, and he would see the others to friends or friends of friends."
Rocha Lawrence, aged 21, admitted to a string of drugs charges including importing a class A drug, supplying a controlled drug of class A (MDMA), supplying a controlled drug of class C and possessing a controlled drug of class B (ketamine) at an earlier hearing.
Defending, Brigid Baillie, said: "This is a young man who has bought drugs over the internet and was supplying or sharing them with his university friends.
"This was not street dealing in the criminal sense of the term.
"On occasion it would be 'have you got any pills for tonight' or 'I've got some pills for tonight'.
"There was clearly an element of sharing, as it appears from the text messages found."
Ms Baillie told the court that Rocha Lawrence, of Portland Tower Lane, Sheffield was on course for a first-class degree, and had got caught up with taking drugs after living in a student house where taking illegal substances was 'normalised'.
She added: "He's done something very, very stupid and he realises he's jeopardised [his degree]."
Ms Baillie continued by saying Rocha Lawrence had since removed himself from that environment and into a new shared house that was a short walk from the university.
Recorder Angus Withington sentenced Rocha Lawrence to two years in prison, suspended for two years, and ordered him to complete 300 hours of unpaid work.
He told Rocha Lawrence: "You are a person of good character and have never previously been before the courts. I'm satisfied that you have demonstrated an amount of remorse.
"You have done work in your community and have taken steps to be treated for your drug problems. These are matters which allow me to suspend your sentence."
Recorder Withington also ordered Rocha Lawrence to complete a 20-day rehabilitation activity requirement and made him the subject of a six-month curfew that prohibits him from leaving the house between 6pm and 6am.