Derek Mackay says he "behaved foolishly" after a newspaper reports he sent hundreds of messages to a 16-year-old boy.