System.Net.Http.HttpRequestException 用于发送网格发送电子邮件

System.Net.Http.HttpRequestException for Send grid send email

我们在某些情况下调用 Sendgrid 发送电子邮件功能时遇到以下异常。 我们一天收到 50,000 封电子邮件中的 20-30 封例外情况。

System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: Unknown error (0xffffffff)     
at System.Net.Sockets.Socket.BeginConnectEx(EndPoint remoteEP, Boolean flowContext, AsyncCallback callback, Object state)     
at System.Net.Sockets.Socket.UnsafeBeginConnect(EndPoint remoteEP, AsyncCallback callback, Object state)     
at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception)     --- 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.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)     
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)     
at SendGrid.Helpers.Reliability.RetryDelegatingHandler.<SendAsync>d__4.MoveNext()  --- End of stack trace from previous location where exception was thrown ---     
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)     
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)     
at SendGrid.BaseClient.<MakeRequest>d__20.MoveNext()  --- End of stack trace from previous location where exception was thrown ---     
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)     
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)     
at SendGrid.BaseClient.<RequestAsync>d__21.MoveNext()  --- End of stack trace from previous location where exception was thrown ---     
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)     
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)     
at SendGrid.BaseClient.<SendEmailAsync>d__22.MoveNext()  --- End of stack trace from previous location where exception was thrown ---     
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)     
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)    

我发送电子邮件的代码是

var client = new SendGridClient("API Key");
var response = await client.SendEmailAsync(message);

任何人都可以帮助我如何添加更多验证以避免这种情况。

到目前为止,重试逻辑对我有用。

catch (Exception ex)
{
    if (--retryMax == 0)
    {
        throw;
    }
    else
    {
        await Task.Delay(1000);
    }
    return null;
}