System.Net.Http.HttpRequestException 在 C# 中调用 Gmail 服务 API
System.Net.Http.HttpRequestException while invoking Gmail service API in C#
在C#中使用GmailAPI时随机出现以下异常。我的代码在几个小时内工作正常,没有任何问题。以下异常以随机方式发生。要解决此问题,我需要重新启动我的应用程序或重新启动 PC。重新启动后,一切都按预期正常工作。我想知道如何永久修复此异常。
03-Jun-2021 17:18:13,755 [INFO ] MailApi+<GmailSendUsingREST>d__27 MoveNext - Email notification send error System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.WebException: The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel. ---> System.Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedure.
at System.Net.TlsStream.EndWrite(IAsyncResult asyncResult)
at System.Net.ConnectStream.WriteHeadersCallback(IAsyncResult ar)
--- 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 Google.Apis.Http.ConfigurableMessageHandler.<SendAsync>d__69.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 Google.Apis.Requests.ClientServiceRequest`1.<ExecuteUnparsedAsync>d__34.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Google.Apis.Requests.ClientServiceRequest`1.Execute()
代码:
var service = new GmailService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
var result = service.Users.Messages.Send(msg, "me").Execute();
异常发生在 Messages.Send。
更新:
请在 Whosebug 上查看 this thread 以了解可能的情况。
嗯,有几点需要注意。
我认为 Gmail 对您每天可以发送的电子邮件有限制。限制数量取决于您拥有的帐户类型。
确保为安全连接打开 SSL 或 TLS。
基础连接已关闭:可能是因为您可以访问互联网但没有数据包来访问网络服务。
System.Security.Authentication.AuthenticationException: 确保您的身份验证凭据正确。最后,
如果您使用 gmail SMTP 发送电子邮件,请确保在您使用的帐户中打开安全性较低的应用程序访问权限。
希望对您有所帮助。编码愉快!
在C#中使用GmailAPI时随机出现以下异常。我的代码在几个小时内工作正常,没有任何问题。以下异常以随机方式发生。要解决此问题,我需要重新启动我的应用程序或重新启动 PC。重新启动后,一切都按预期正常工作。我想知道如何永久修复此异常。
03-Jun-2021 17:18:13,755 [INFO ] MailApi+<GmailSendUsingREST>d__27 MoveNext - Email notification send error System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.WebException: The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel. ---> System.Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedure.
at System.Net.TlsStream.EndWrite(IAsyncResult asyncResult)
at System.Net.ConnectStream.WriteHeadersCallback(IAsyncResult ar)
--- 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 Google.Apis.Http.ConfigurableMessageHandler.<SendAsync>d__69.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 Google.Apis.Requests.ClientServiceRequest`1.<ExecuteUnparsedAsync>d__34.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Google.Apis.Requests.ClientServiceRequest`1.Execute()
代码:
var service = new GmailService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
var result = service.Users.Messages.Send(msg, "me").Execute();
异常发生在 Messages.Send。
更新:
请在 Whosebug 上查看 this thread 以了解可能的情况。
嗯,有几点需要注意。
我认为 Gmail 对您每天可以发送的电子邮件有限制。限制数量取决于您拥有的帐户类型。
确保为安全连接打开 SSL 或 TLS。
基础连接已关闭:可能是因为您可以访问互联网但没有数据包来访问网络服务。
System.Security.Authentication.AuthenticationException: 确保您的身份验证凭据正确。最后,
如果您使用 gmail SMTP 发送电子邮件,请确保在您使用的帐户中打开安全性较低的应用程序访问权限。
希望对您有所帮助。编码愉快!