对于所有消息,Message.Payload始终为null。我如何获得这些数据?

时间:2016-04-01 20:18:49

标签: gmail-api

它返回正确数量的消息,但是填充的唯一字段是Id和ThreadId。其他一切都是空的

        var service = new GmailService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = ApplicationName,
        });

        ListMessagesResponse response = service.Users.Messages.List(emailAddress).Execute();

        IList<Google.Apis.Gmail.v1.Data.Message> messages = response.Messages;

        Console.WriteLine("Messages:");
        if (messages != null && messages.Count > 0)
        {
            foreach (var message in messages)
            {
                Console.WriteLine("{0}", message.Payload.Body);
                Console.WriteLine();
            }
        }
        else
        {
            Console.WriteLine("No Messages found.");
        }

1 个答案:

答案 0 :(得分:7)

Messages.List()仅返回消息和线程ID。要检索邮件内容,您需要为您感兴趣的每条邮件调用Get()foreach循环示例的更新版本:

foreach (var message in messages)
{
    Message m = service.Users.Messages.Get("me", message.Id).Execute();
    // m.Payload is populated now.
    foreach (var part in message.Payload.Parts)
    {
        byte[] data = Convert.FromBase64String(part.Body.Data);
        string decodedString = Encoding.UTF8.GetString(data);
        Console.WriteLine(decodedString);
    }
}