Search code examples
c#gmailconsole-application

Failure sending mail :C#:Gmail: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host


I am getting error while sending email using C# console application .it was working perfectly before few but it started giving me the error below is my code

            smtp.Host = "smtp.gmail.com"; 
            smtp.Port = Convert.ToInt32(587);
            smtp.EnableSsl = true;
            smtp.UseDefaultCredentials = false;
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtp.ServicePoint.MaxIdleTime = 60;
            smtp.Timeout = 20000;
            smtp.Credentials = new NetworkCredential(UserName, Password);

            ServicePointManager.ServerCertificateValidationCallback = delegate (object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
            { return true; };
            smtp.Send(msg);

Error

enter image description here

I have also updated 'Allow Less Secure Apps' to On in google settings does anyone has any idea


Solution

  • I have fixed the issue by own. I have to give the AD service account proper permission to send SMTP as any from address