【发布时间】:2015-11-16 15:53:46
【问题描述】:
我的意思是得到一个将像这种格式构建的列表:
从到消息
例如在第一个索引的列表中,我会看到:
发件人:Daniel 收件人:Jhon 消息:hello world
在我现在使用的这段代码中,我在结果变量中得到的只是 1445 条消息,这在我的 gmail.com 帐户中很奇怪,我看到收件箱中有 374 封电子邮件,所以为什么变量结果总是返回1445 ?我尝试将 JSON 文件更改为我的 gmail 帐户,然后更改为我朋友的 gmail 帐户,在这两种情况下它都返回 1445。我怎样才能让它根据 JASON 文件中的 gmail 帐户返回电子邮件数量?
在结果列表变量中,例如在索引 0 上,当我用鼠标站在它上面时,我只能得到消息 Id 和消息 ThreadId,但是我如何获取 From 和 To 以及消息正文/文本?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Google.Apis.Gmail.v1.Data;
using Google.Apis.Gmail.v1;
using Google.Apis.Gmail;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using Google.Apis.Upload;
using System.Threading;
using System.IO;
using Google.Apis.Util.Store;
namespace Google_Gmail
{
public partial class Form1 : Form
{
static string[] Scopes = { GmailService.Scope.GmailReadonly };
static string ApplicationName = "Youtube Uploader";
public Form1()
{
InitializeComponent();
ListMessages(GmailServices(), "me", "");
}
private GmailService GmailServices()
{
UserCredential credential;
using (var stream =
new FileStream(@"C:\jason file\client_secrets.json", FileMode.Open, FileAccess.Read))
{
string credPath = System.Environment.GetFolderPath(
System.Environment.SpecialFolder.Personal);
credPath = Path.Combine(credPath, ".credentials");
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
Console.WriteLine("Credential file saved to: " + credPath);
}
// Create Gmail API service.
var service = new GmailService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
return service;
}
public static List<Google.Apis.Gmail.v1.Data.Message> ListMessages(GmailService service, String userId, String query)
{
List<Google.Apis.Gmail.v1.Data.Message> result = new List<Google.Apis.Gmail.v1.Data.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;
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
【问题讨论】:
-
未标记答案。
标签: c# .net winforms google-api gmail-api