A man who was caught trying to send a picture of his genitals to a teenage girl has been sentenced in court.