LONDON — Scotland’s finance minister has resigned, hours before he was due to deliver his annual budget, after he reportedly sent hundreds of messages to a 16-year-old boy on social media.

Derek Mackay quit Thursday, saying he had “behaved foolishly.”

The Scottish Sun newspaper reported that 42-year-old Mackay sent the teen more than 270 messages on Facebook and Instagram, telling the boy he was “cute” and offering to take him to a rugby match and to dinner. Mackay is not accused of breaking the law.

“Serving in government has been a huge privilege and I am sorry to have let colleagues and supporters down,” Mackay said in a statement.

Mackay was a rising star in the Scottish National Party, which runs Scotland’s semi-autonomous Edinburgh-based administration.

He had been seen as a potential successor to First Minister Nicola Sturgeon as leader of the SNP, which advocates independence from the United Kingdom for the nation of 5.5 million.