3

It is returning the correct number of message, but the only fields that are populated are Id and ThreadId. Everything else is null

        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.");
        }
Samuel Elrod
  • 337
  • 1
  • 3
  • 13

1 Answers1

12

Messages.List() only returns message and thread ids. To retrieve message contents, you need to invoke Get() for each message you are interested in. Updated version of your foreach loop example:

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

Note: You may need to run a string replace on the part.Body.Data string. See this post for instructions.

Dan Beaulieu
  • 19,406
  • 19
  • 101
  • 135
  • You wouldn't know how to get the info from Message.Paylod.Body.Data to text would you? – Samuel Elrod Apr 01 '16 at 21:12
  • 1
    Updated code snippet with example of parsing mime parts. – Brandon Jewett-Hall Apr 01 '16 at 21:22
  • 1
    Yeah, thats what I've been trying; I get: The input is not a valid Base-64 string as it contains a non-base 64 character, – Samuel Elrod Apr 01 '16 at 21:23
  • 1
    Nevermind, it was a padding issue, fixed by this: switch (s.Length % 4) // Pad with trailing '='s { case 0: break; // No pad chars in this case case 2: s += "=="; break; // Two pad chars case 3: s += "="; break; // One pad char default: throw new System.Exception( "Illegal base64url string!"); } – Samuel Elrod Apr 01 '16 at 21:29
  • 1
    >Messages.List() only returns message and thread ids. \n Can you link to where this is documented please? – hermancain Jul 16 '17 at 18:41
  • @Brandon Jewett-Hall I went to Gmail API documentation and there is no mention on the messages.list method that explains I should use the Get() function. It says completely the opposite, it gives a field to choose for fields of message you would like to fetch and payload is included on that list although you can check on the api itself it wont work. I honestly think it is a bug on the API. https://developers.google.com/apis-explorer/#p/gmail/v1/gmail.users.messages.list – Murilo Nov 01 '17 at 20:13