var httpClient = new HttpClient(
new HttpClientHandler()
{
Proxy = new WebProxy(webProxy),
UseProxy = true
}
);
var sg = new SendGrid.SendGridClient(apiKey: apiKey);
sg = new SendGrid.SendGridClient(httpClient, apiKey);
var mail = new SendGridMessage();
mail.AddTo(new EmailAddress(retsendemail.To));
mail.From = new EmailAddress(retsendemail.From);
mail.ReplyTo = new EmailAddress(retsendemail.From);
mail.Subject = retsendemail.Subject;
mail.AddContent("text/html", retsendemail.Body);
var response = sg.SendEmailAsync(mail).ConfigureAwait(false).GetAwaiter().GetResult();
log.Info("Send Response status code -> " + response.StatusCode.ToString());
log.Info("Send Response header -> " + response.Headers.GetValues("X-Message-Id").ToString());
=============================================================
An error occurred while sending the request.
System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive. ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
at System.Net.Sockets.Socket.EndReceive(IAsyncResult asyncResult)
at System.Net.Sockets.NetworkStream.EndRead(IAsyncResult asyncResult)
--- End of inner exception stack trace ---
at System.Net.Sockets.NetworkStream.EndRead(IAsyncResult asyncResult)
at System.Net.Connection.ReadCallback(IAsyncResult asyncResult)
--- End of inner exception stack trace ---
at System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult asyncResult, TransportContext& context)
at System.Net.Http.HttpClientHandler.GetRequestStreamCallback(IAsyncResult ar)
--- End of inner exception stack trace ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at SendGrid.SendGridClient.
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at SendGrid.SendGridClient.
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at SendGrid.SendGridClient.
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
Hello @jestoya,
It looks like you may have an issue with your server talking to our server. For this type of issue, it's best to reach out to our support team so that they may look deeper into your account. It is also possible, you may be running into this issue.
With Best Regards,
Elmer Thomas
Well the support team suggestion is to go here. And now I need to go back to support team? This is not happening in all the request. 1 or 2 out of 10 request is failing. I even have 700 emails that has been delivered in my past testing so I don't think this is on our side.

Hello @jestoya,
When you reach out to support, please reference this issue. I suspect your issue is related.
With Best Regards,
Elmer
Hi @jestoya,
I had similar issue and solved it by setting TLS 1.2 as the default connection.
Just below creating the HTTPClient add the following:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
Thanks for helping out @12c4IT!
I'm getting the same issue, but I don't know where to go to apply that HTTPClient fix
Most helpful comment
Hi @jestoya,
I had similar issue and solved it by setting TLS 1.2 as the default connection.
Just below creating the HTTPClient add the following:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;