Search code examples
c#gmailgmail-api

How to get Gmail subjects using Gmail API in C#


I am making an app that can get all the e-mails in Gmail Inbox from a C# console application.

I have this code below that prints out the console the email body contents:

UsersResource.ThreadsResource.ListRequest threadRequest = service.Users.Threads.List("me");
IList<Google.Apis.Gmail.v1.Data.Thread> threads = threadRequest.Execute().Threads;

foreach(var thread in threads)
{
     //print gmail body content
     Console.WriteLine("{0}", thread.Snippet);
}

So how do I get gmail subjects instead? Any contribution will be appriciated!


Solution

  • public string GetMail(){
    GmailService service = (GmailService)HttpContext.Current.Session["service"];
    Message messageFeed = service.Users.Messages.List("me").Execute().Messages.First();
    UsersResource.MessagesResource.GetRequest getReq = new UsersResource.MessagesResource.GetRequest(service, "me", messageFeed.Id);
     //"raw": Returns the full email message data with body content in the raw field as a base64url encoded string; the payload field is not used. 
    getReq.Format = UsersResource.MessagesResource.GetRequest.FormatEnum.Raw;
     Message message = getReq.Execute();
    return message.Raw;
    }
    

    Getting more data from messages.get in C#

    Gmail API

    Better solution:

    Read the Gmail Inbox Feed with .NET and OAuth