Rewrite sanitizeMailAddresses

It should now be more robust and shouldn't crash when handling strange input.