从Google API获取未读电子邮件 [英] Get Unread emails from Google API

查看:39
本文介绍了从Google API获取未读电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Google API来获取未读电子邮件的数量,但无法执行.非常感谢您的帮助.我没有收到任何错误,但计数与gmail中显示的实际数字不匹配.

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.

 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<Message> lst = ListMessages(gmailservice, "xxx@gmail.com", "IN:INBOX IS:UNREAD");
            }
            catch (Exception e)
            {
                Console.WriteLine("An error occurred: " + e.Message);
            }
        }
        catch (Exception ex)
        {

        }       

推荐答案

您可以使用示例中的rel ="nofollow"> ListMessages方法(出于完整性考虑,已包含在内)用于搜索:

You can use the ListMessages method from the API example (included for completeness) for searching:

private static List<Message> ListMessages(GmailService service, String userId, String query)
        {
            List<Message> result = new List<Message>();
            UsersResource.MessagesResource.ListRequest request = service.Users.Messages.List(userId);
            request.Q = query;

            do
            {
                try
                {
                    ListMessagesResponse response = request.Execute();
                    result.AddRange(response.Messages);
                    request.PageToken = response.NextPageToken;
                }
                catch (Exception e)
                {
                    Console.WriteLine("An error occurred: " + e.Message);
                }
            } while (!String.IsNullOrEmpty(request.PageToken));

            return result;
        }

您可以使用此搜索方法查找未读邮件,例如:

You can use this search method to find unread messages, for example like this:

List<Message> unreadMessageIDs = ListMessages(service, "me", "is:unread");

q参数(查询)可以是各种各样的东西(与Web界面顶部的gmail搜索栏相同),如此处所述:

The q parameter (query) can be all kinds of stuff (it is the same as the gmail search bar on the top of the web interface), as documented here: https://support.google.com/mail/answer/7190?hl=en.

请注意,您仅设置了Message对象的几个参数.如果要检索消息,则必须使用 GetMessage方法从api :

Note that you only a few parameters of the Message objects are set. If you want to retreive the messages you'll have to use GetMessage method from the api:

public static Message GetMessage(GmailService service, String userId, String messageId)
  {
      try
      {
          return service.Users.Messages.Get(userId, messageId).Execute();
      }
      catch (Exception e)
      {
          Console.WriteLine("An error occurred: " + e.Message);
      }

      return null;
  }

我同意该API并非直截了当,并且缺少许多功能.

I agree that the API is not straight forward and misses a lot of functionality.

这篇关于从Google API获取未读电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆