Improved error handling for sending messages.