A Royal Navy submariner who tried to pass secrets to the Russians that could have undermined the UK's national security has been jailed for eight years by an Old Bailey judge who condemned his attempted betrayal.

Edward Devenney, 30, had not realised that the two men to whom he was trying to give secret intelligence were not members of the Russian secret service, but agents working for MI5.

The trap ensured that Devenney did not pass any material that could have endangered Britain's nuclear submarine fleet, but Mr Justice Saunders said this was a serious case and that the petty officer, from Country Tyrone, Northern Ireland, needed to receive "a proper punishment".

"He did supply details of movements and operations carried out and to be carried out by nuclear submarines," he said.

"The court has to mark the defendant's attempted betrayal of not only his country but also his colleagues who must feel great anger at his behaviour."

Devenney had been in the Royal Navy for more than 11 years and had worked on three of the UK's four Trident submarines when he hatched his plan late 2011. He was not motivated by money, but by a sense of anger and betrayal that his career had stalled, the court was told.

At the time he was drinking heavily and had suffered bouts of depression following a court case in 2010 during which he had been accused of rape. Devenney was cleared.

The judge admitted the allegation had clearly affected Devenney's behaviour, but said that he was "completely in control of himself" by the time he had set up the meeting with the two "Russian" men in January this year. By then Devenney had already made a series of phone calls to the Russian embassy and had photographed code-breaking technology on board the Trident submarine HMS Vigilant using his girlfriend's mobile phone.

Mark Dennis, prosecuting, said Devenney was contacted by a man called Dimitri, a British agent who was posing as a Russian spy. At first Devenney appeared to have seen through the ruse, telling the agent: "Your accent sounds remarkably fake, like British intelligence."

But Devenney persevered and was introduced to another MI5 agent, who claimed his name was Vladimir.

Devenney agreed to meet them at the British Museum in central London on 28 January and accompanied the two agents to a nearby hotel room.

It was there Devenney disclosed details about the sailing dates of HMS Vigilant, which was due to travel to the west Atlantic for missile testing. He also had information about HMS Vengeance and a top-secret operation undertaken by HMS Trafalgar.

The court was told pictures he had taken were an "essential piece of the jigsaw" that would allow enemies to break navy codes used to communicate with Nato allies. The codes have been in use for decades and have yet to be compromised.

Dennis said disclosing sailing dates could allow enemies to record and identify the "unique signature" of British submarines.

"Once the signature is captured and identified, then that submarine has lost its most critical capability, namely the ability to be deployed covertly and without detection," he said.

During sentencing, the judge said Devenney had "made determined efforts to enter into an agreement to supply secret information to representatives of another country". He added: "The reason he later gave for his actions was that he wished to get his own back on the Royal Navy who he considered had treated him badly. The objective evidence is that the Royal Navy treated him well."

Devenney admitted collecting information about cryptography technology between 18 November last year and 7 March this year.

He also pleaded guilty to misconduct in a public office by "attempting to set up and continue a covert relationship with a foreign power with the intention to harm the Royal Navy".

Outside court, Devenney's lawyer Richard Cannon read a statement on his behalf.

"I am deeply sorry for the hurt and shame that I have brought on my family and loved ones," it said. "I deeply regret my actions and the effect they have had on the Submarine Service and colleagues."

Mari Reid, unit head for the CPS counter-terrorism division, said: "This was a classic story of betrayal.

"Edward Devenney was employed by the Royal Navy to protect this country from potential threats to our security. Instead, he pursued a course of conduct likely to put his country at risk."