Fix issue where socket connection to mailserver was not closed.
Checkpointing progress of work to add Template management