如何使用带有C#的GMail API获取电子邮件HTML

时间:2018-12-20 12:10:30

标签: c# gmail gmail-api

我编写的Windows服务应该从Gmail任务中接收电子邮件

enter image description here

这是我的测试方法:

            UserCredential credential;
            UserCredential credentialGmail;

            using (var stream =
                new FileStream("credentials.json", FileMode.Open, FileAccess.Read))
            {
                string credPath = "token.json";
                credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    Scopes,
                    "user",
                    CancellationToken.None,
                    new FileDataStore(credPath, true)).Result;
            }
            using (var stream =
                new FileStream("credentialsGmail.json", FileMode.Open, FileAccess.Read))
            {
                string credPath = "tokenGmail.json";
                credentialGmail = GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    ScopesGmail,
                    "user",
                    CancellationToken.None,
                    new FileDataStore(credPath, true)).Result;
            }

            // Create Google Tasks API service.
            var service = new TasksService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = ApplicationName,
            });
            var service2 = new GmailService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credentialGmail,
                ApplicationName = ApplicationName,
            });

            // Define parameters of request.
            TasklistsResource.ListRequest listRequest = service.Tasklists.List();
            listRequest.MaxResults = 1000;
            // List task lists.
            IEnumerable<TaskList> taskLists = listRequest.Execute().Items.Where(p => p.Title == "Test");
            if (taskLists != null && taskLists.Count() > 0)
            {
                foreach (var taskList in taskLists)
                {
                    TasksResource.ListRequest tasksListRequest = service.Tasks.List(taskList.Id);
                    var tasks = tasksListRequest.Execute().Items;
                    if (tasks != null)
                    {
                        foreach (var item in tasks)
                        {
                            if ((item.Links != null) 
                                && (item.Links.Count == 1) 
                                && (item.Links[0].Type == "email"))
                            {
                                var uri = item.Links[0].Link.Split('/').Last();
                                var getEmailRequest = service2.Users.Messages.Get("me", uri);
                                var getEmailResponse = getEmailRequest.Execute();
                            }
                        }
                    }
                }
            }

它搜索在列表“测试”中添加了电子邮件的任务。找到正确的任务后,它将接收任务中的电子邮件。为此,我使用 Users.Messages.Get 。 我几乎收到了所有电子邮件信息。但是我找不到电子邮件和附件的HTML。我有几个问题:

  1. 如何通过GMail API获取消息的HTML(我在代码段中有文本,也检查了Payload.Headers,但在那里找不到)?
  2. 如何将任务状态设置为“已完成”?

0 个答案:

没有答案