【问题标题】:System.Net.Sockets.SocketException (10061)System.Net.Sockets.SocketException (10061)
【发布时间】:2020-10-31 06:08:04
【问题描述】:

我使用 SendgridAPI 向用户发送确认电子邮件。我在 sendgrid 应用程序 (https://app.sendgrid.com) 中创建 API_key 并将其 api 安装在我的项目中。

我在c#中的执行代码是这样的:

public static async Task<bool> Execute(string userEmail, string userName, string plainTextContent, string htmlContent, string subject)
        {
            var apiKey = "SG.E43545453453URY1A.tq8j-fduDHDKONPuf_Gp1W35IkQjI-DzRsAsmgdfdfg5DgEBM";
            var client = new SendGridClient(apiKey);
            var from = new EmailAddress("test@example.com", "Mehran");
            var to = new EmailAddress(userEmail, userName);
            var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent);
            var response = await client.SendEmailAsync(msg);
            return await Task.FromResult(true);
        }

我称之为这个方法。运行应用程序和此方法时,抛出以下异常:

System.Net.Http.HttpRequestException: No connection could be made because the target machine actively refused it.
 ---> System.Net.Sockets.SocketException (10061): No connection could be made because the target machine actively refused it.
   at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
   --- End of inner exception stack trace ---
   at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.ConnectAsync(HttpRequestMessage request, Boolean allowHttp2, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.CreateHttp11ConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.GetHttpConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
   at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Net.Http.DiagnosticsHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at SendGrid.Helpers.Reliability.RetryDelegatingHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
   at SendGrid.BaseClient.MakeRequest(HttpRequestMessage request, CancellationToken cancellationToken)
   at SendGrid.BaseClient.RequestAsync(Method method, String requestBody, String queryParams, String urlPath, CancellationToken cancellationToken)
   at SendGrid.BaseClient.SendEmailAsync(SendGridMessage msg, CancellationToken cancellationToken)
   at AnguralToApi.Services.SendGridAPI.Execute(String userEmail, String userName, String plainTextContent, String htmlContent, String subject) in E:\Asp.Net Project\AnguralToApi\AnguralToApi\Services\SendGridAPI.cs:line 19
   at AnguralToApi.Controllers.AccountController.Register(RegisterModelView model) in E:\Asp.Net Project\AnguralToApi\AnguralToApi\Controllers\AccountController.cs:line 66
   at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.TaskOfIActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Logged|12_1(ControllerActionInvoker invoker)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeInnerFilterAsync>g__Awaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker)
   at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
   at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

HEADERS
=======
Accept: */*
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Length: 91
Content-Type: application/json
Host: localhost:49839
User-Agent: PostmanRuntime/7.26.1
Postman-Token: 6131aae5-b461-4be5-bc78-3aef28950a5e

你能帮帮我吗?!

【问题讨论】:

    标签: connection sendgrid confirmation


    【解决方案1】:

    首先,切勿将 API 密钥放入公共论坛或检查源代码管理,因为这可能意味着有人可能会从您的帐户中将其用作垃圾邮件。

    其次,连接错误的类型让我认为你被防火墙,防病毒等阻止连接到sendgrid。另一种可能是 api.sendgrid.com 解析到的机器不正确。

    【讨论】:

      猜你喜欢
      • 2011-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-22
      • 1970-01-01
      • 2019-05-13
      • 2020-01-21
      相关资源
      最近更新 更多