我编写的Windows服务应该从Gmail任务中接收电子邮件
这是我的测试方法:
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。我有几个问题: