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);