Added error handling to the mail code