简体   繁体   中英

Accessing Message Content from Gmail API

I am having trouble getting message content to show up using the gmail API. I have connected to the mailbox, and am generating a list of messages, but all values associated with the messages, aside from the ID, are null.

The code to generate the list of messages is cut from the sample code on the gmail API website:

public static List<Message> ListMessages(GmailService service, String userId, String query)
    {
        List<Message> result = new List<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;
    }

And the code to print the messages is:

List<Message> messages = ListMessages(service, "me", ""); 
        Console.WriteLine("Messages from user's inbox:");
        foreach(Message m in messages)
        {
            string snippet = m.Snippet;
            Console.WriteLine("Message: {0}", snippet);
        }

But once again, when I place a breakpoint at the print line and inspect the list of messages, all values associated with the content of the messages are null.

I figured it out; I'll leave it up because it wasn't exactly obvious. Had to add a get statement within the foreach loop to explicitly "get" the message using the Id from the list.

foreach(Message m in messages)
        {
            Message m2 = service.Users.Messages.Get("me", m.Id).Execute();
            Console.WriteLine("Message: {0}", m2.Snippet);
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM