title: Get Unread emails from Google API
body: <p>I'm trying to get the count of unread email using google API, but not able. ANy help is highly appreciated. I'm not getting any error, but the count doesnt match the actual number shown in gmail.</p>
  
  <pre><code> try
          {
              String serviceAccountEmail = "xxx@developer.gserviceaccount.com";
              var certificate = new X509Certificate2(@"C:\Projects\xxx\xyz\API Project-xxxxx.p12", "notasecret", X509KeyStorageFlags.Exportable);
  
              ServiceAccountCredential credential = new ServiceAccountCredential(
              new ServiceAccountCredential.Initializer(serviceAccountEmail)
              {
                  User = "xxx@gmail.com",
  
                  Scopes = new[] { Google.Apis.Gmail.v1.GmailService.Scope.GmailReadonly }
              }.FromCertificate(certificate));
  
  
              var gmailservice = new Google.Apis.Gmail.v1.GmailService(new BaseClientService.Initializer()
              {
                  HttpClientInitializer = credential,
                  ApplicationName = "GoogleApi3",
              });
  
              try
              {                 
  
                  List&lt;Message&gt; lst = ListMessages(gmailservice, "xxx@gmail.com", "IN:INBOX IS:UNREAD");
              }
              catch (Exception e)
              {
                  Console.WriteLine("An error occurred: " + e.Message);
              }
          }
          catch (Exception ex)
          {
  
          }       
  </code></pre>