Fix NPE caused by misplaced bracket

create method to send mail instead of using same code multiple times.