0

The following exception occurs randomly when using Gmail API in C#. My code works fine without any issues for few hours. The below exception occurs in a random manner. To resolve the issue, I need to just restart my application or restart the PC. After restart, everything works fine as expected. I would like to know how to fix this exception permanently.

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()

Code:

 var service = new GmailService(new BaseClientService.Initializer()
 {
        HttpClientInitializer = credential,
        ApplicationName = ApplicationName,
 });

 var result = service.Users.Messages.Send(msg, "me").Execute();

Exception occurs at Messages.Send.

Update:

Please see this thread on Stackoverflow for possible scenarios.

pradeep
  • 175
  • 1
  • 13
  • Generally web calls should be built to retry since intermittent issues are common. Polly is good framework to assist you with making web calls more robust. What I don't understand is why it takes the whole process down such that you have to restart. Maybe your are not Disposing something that really needs disposing. – Crowcoder Jun 03 '21 at 18:30

1 Answers1

0

Well, there are few things you have to take note of.

  1. I think Gmail has limits of emails you can send per day. The limit number depends on the type of account you have.

  2. Make sure that SSL or TLS is turned on for secure connection.

  3. The underlying connection was closed: may be caused when you have access to internet but you are out of data bundle for accessing a web service.

  4. System.Security.Authentication.AuthenticationException: make sure your authentication credentials are correct. And lastly,

  5. If you're sending emails using gmail SMTP, make sure you turn on less secure app access in the account you're using.

I hope this helps. Happy Coding!

Pat Nadis
  • 80
  • 9
  • Thanks for the quick reply. Please find my comments below. 1) If there are limits, it should not work after I restart the application/PC. 2) Application is running in the same PC always. Since the connection is working as expected most of the time, this error occurs only randomly. If my security/certificates are not proper, I believe it will not work even once. 3) How do I confirm whether I'm out of data bundle for accessing a web service. In this case, I'm using Gmail REST API. – pradeep Jun 03 '21 at 18:33
  • I really don't know how you're connecting to the internet, maybe via WiFi, Ethernet, or cellular network. Which ever way make sure that it allows you access to ping the web. It could be that each time it fails you already lost access to the internet. You can also check my number 5 option to make sure you're doing things right. – Pat Nadis Jun 03 '21 at 19:24