Scotland’s finance chief has resigned after a newspaper report that he had sent hundreds of messages to a 16-year-old boy.