I am getting this error without making any changes to the code. Previously, it was working fine.
Depth: 0<br />Exception Type:System.Net.Mail.SmtpException<br />Exception: The server committed a protocol violation The server response was: <br />Inner Source: System<br />Stack Trace: at System.Net.Mail.SendMailAsyncResult.End(IAsyncResult result)
at System.Net.Mail.SmtpClient.SendMailCallback(IAsyncResult result)
My code:
private async Task SmtpClientSendMail(MailMessage mailMessage)
{
using (SmtpClient smtpClient = new SmtpClient("smtp.office365.com"))
{
try
{
smtpClient.UseDefaultCredentials = false;
smtpClient.Port = 587;
smtpClient.Credentials = new System.Net.NetworkCredential(emailUserNameCredentials, emailPasswordCredentials);
smtpClient.EnableSsl = true;
smtpClient.Timeout = 200000;
await smtpClient.SendMailAsync(mailMessage);
}
catch (Exception ex)
{
}
}
}
As suggested in System.Net.WebException: The server committed a protocol violation
I have added this to my App.config directly in the section:
<system.net>
<settings>
<httpWebRequest useUnsafeHeaderParsing="true"/>
</settings>
</system.net>
and I have added <httpProtocol allowKeepAlive="false" />
to web config as well. Still, I am getting the error and emails were not sent.
Sometimes, It is working without getting errors. Why does it no longer work as it used to be?
Could indeed be TLS version problem, try this:
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;