A submariner who offered to pass on naval secrets to Russian spies was on the verge of being sacked, the Old Bailey has heard.
Petty officer Edward Devenney, 30, said he was disillusioned with the Royal Navy because his promotion hopes had been dashed through defence cuts.
But he was drinking heavily, had bouts of depression and had just been cleared of a rape charge.
He asked for his training course for promotion to be deferred for a year but his absences without leave and conduct had led to a warning that he would be sacked if it continued, the court heard.
It was only through the vigilance of MI5 officers who mounted a sting operation where they pretended to be Russian spies that national security was not harmed, said Mark Dennis QC, prosecuting.
Mr Dennis said: "The potential damage could have been considerable and could have harmed the safety and security of the United Kingdom."
Devenney, originally from Northern Ireland, has pleaded guilty to breaching the Official Secrets Act and misconduct in public office.
He contacted the Russian Embassy offering information and later met two men he thought were Russian agents in London.
But they were British intelligence officers who recorded their meeting, during which Devenney said he was "pissed off with the Navy" and did not want payment.
He was arrested in March in Plymouth where he was based.