从 gmail 获取消息 ID 非常慢

getting messaged id's from gmail is very slow

我已经使用 .net 4.5 版创建了 class 库,语言是 c#。从 gmail API 中提取 6 封电子邮件需要几分钟以上的时间。 下面是我用来获取邮件详细信息的代码

public Message GetMailDetails(GmailService service, string EmailId, string MessageID)
{
            try
            {
                var a = service.Users.Messages.Get(EmailId, MessageID);
                a.Format = UsersResource.MessagesResource.GetRequest.FormatEnum.Metadata;
                var retMessage = a.Execute();


                //a.Format = UsersResource.MessagesResource.GetRequest.FormatEnum.Metadata;
                //retMessage = a.Execute();
                //a.Format = UsersResource.MessagesResource.GetRequest.FormatEnum.Minimal;
                //retMessage = a.Execute();
                //a.Format = UsersResource.MessagesResource.GetRequest.FormatEnum.Raw;
                //retMessage = a.Execute();
                return service.Users.Messages.Get(EmailId, MessageID).Execute();
            }
            catch (Exception e)
            {
                return null;
            }
 }

您可以尝试使用名为 Partial response the easiest way to test this is to go through the try me page try me 的东西,您需要一个消息 ID 来测试它

message.get 的完整回复如下所示

{
  "id": "1742abfd3a4f1c5",
  "threadId": "1742a8c81816e51",
  "labelIds": [
    "CHAT"
  ],
  "snippet": "I'm surprised nobody questioned me when I said that the tag was 2 weeks old, but clearly has questions from longer than 2 weeks ago. {:",
  "payload": {
    "partId": "",
    "mimeType": "text/html",
    "filename": "",
    "headers": [
      {
        "name": "From",
        "value": "Axxxx"
      }
    ],
    "body": {
      "size": 139,
      "data": "SSYjMzk7bSBzdXJwcmlzZWQgbm9ib2R5IHF1ZXN0aW9uZWQgbWUgd2hlbiBJIHNhaWQgdGhhdCB0aGUgdGFnIHdhcyAyIHdlZWtzIG9sZCwgYFybHkgaGFzIHF1ZXN0aW9ucyBmcm9tIGxvbmdlciB0aGFuIDIgd2Vla3MgYWdvLiB7Og=="
    }
  },
  "sizeEstimate": 100,
  "historyId": "6172496",
  "internalDate": "1598445048740"
}

通过将字段参数添加到您的请求字段=id,threadid,响应将如下所示

{
  "id": "1742abfd3a4f16c5",
  "threadId": "1742a8c181816e51"
}

基本上您只请求您想要查看的数据字段。

例子

var request =  service.Users.Messages.Get(EmailId, MessageID)
request.Fields = "id,threadid";
var response = request.Execute();

当您尝试从数组或列表中挑选内容时,字段开始工作可能有点棘手,我真的建议您在 try me 中对其进行测试。