'Getting Error Sending mail via SmtpClient - Unable to read data from the transport connection

I am getting the error "Unable to read data from the transport connection" when sending an email using the SmtpClient. The error does not happen when I am running this in Visual Studio. The error only happens when it is running in IIS. Below is my code:

            System.Net.ServicePointManager.Expect100Continue = false;
            System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
            ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(IgnoreCertificateErrorHandler);

            SmtpClient SmtpServer = new SmtpClient(_serviceSettings.SMTPServer, _serviceSettings.SMTPPort);
            SmtpServer.Port = _serviceSettings.SMTPPort;
            SmtpServer.UseDefaultCredentials = false;
            SmtpServer.Credentials = new System.Net.NetworkCredential(_serviceSettings.SMTPUser, _serviceSettings.SMTPPass);


            MailMessage mail = new MailMessage();
            mail.From = new MailAddress(_serviceSettings.SMTPUser);
            mail.To.Add(_serviceSettings.SMTPErrorTo);
            mail.Subject = "Error Message";
            mail.Body = errorMessage;

            SmtpServer.EnableSsl = _serviceSettings.SMTPUseSSL;

            SmtpServer.Send(mail);


Solution 1:[1]

It ended up being an issue with the SMTP server. One of the email servers in the cluster was having an issue.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Andy