Sendgrid-csharp: An error occurred while sending the request.

Created on 26 Feb 2019  路  6Comments  路  Source: sendgrid/sendgrid-csharp

We have a proxy in our server. Some of the files are being delivered but sometimes its failing when sending the request (sg.SendEmailAsync(mail))

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.d__21.MoveNext()
--- 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.d__22.MoveNext()
--- 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.d__23.MoveNext()
--- 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()

unknown or a waiting for feedback question

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;

All 6 comments

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.
image

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jewpaltz picture jewpaltz  路  3Comments

loganwasif005 picture loganwasif005  路  3Comments

clairernovotny picture clairernovotny  路  4Comments

shervinw picture shervinw  路  4Comments

eat-sleep-code picture eat-sleep-code  路  3Comments