Gmail Inbox Feed with .NET and OAuth

Gmail servers support the standard IMAP and POP protocols for email retrieval but sometimes you only need to know whether there are any new messages in your inbox. Using any of the two protocols mentioned above may seem like an overkill in this scenario and that’s why Gmail also exposes a read only feed called Gmail Inbox Feed which you can subscribe to and get the list of unread messages in your inbox.

The Gmail Inbox Feed is easily accessible by pointing your browser to https://mail.google.com/mail/feed/atom and authenticating with your username and password if you are not already logged in.

Using basic authentication to access the inbox feed doesn’t provide a very good user experience if we want delegated access. In that case, we should instead rely on the OAuth authorization standard, which is fully supported by the Gmail Inbox Feed.

OAuth supports two different flows. With 3-legged OAuth, an user can give access to a resource he owns to a third-party application without sharing his credentials. The 2-legged flow, instead, resembles a client-server scenario where the application is granted domain-wide access to a resource.

Let’s write a .NET application that uses 2-legged OAuth to access the Gmail Inbox Feed for a user in the domain and list unread emails. This authorization mechanism also suits Google Apps Marketplace developers who want to add inbox notifications to their applications.

There is no dedicated client library for this task and the Inbox Feed is not based on the Google Data API protocol but we’ll still use the .NET library for Google Data APIs for its OAuth implementation.

Step-by-Step

First, create a new C# project and add a reference to the Google.GData.Client.dll released with the client library. Then add the following using directives to your code:

using System;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Xml;
using System.Xml.Linq;
using Google.GData.Client;

The next step is to use 2-legged OAuth to send an authorized GET request to the feed URL. In order to do this, we need our domain’s consumer key and secret and the username of the user account we want to access the inbox feed for.

string CONSUMER_KEY = "mydomain.com";
string CONSUMER_SECRET = "my_consumer_secret";
string TARGET_USER = "test_user";

OAuth2LeggedAuthenticator auth = new OAuth2LeggedAuthenticator("GmailFeedReader", CONSUMER_KEY, CONSUMER_SECRET
, TARGET_USER, CONSUMER_KEY);
HttpWebRequest request = auth.CreateHttpWebRequest("GET", new Uri("https://mail.google.com/mail/feed/atom/"));
HttpWebResponse response = request.GetResponse() as HttpWebResponse;

The response is going to be a standard Atom 0.3 feed, i.e. an xml document that we can load into an XDocument using the standard XmlReader class:

XmlReader reader = XmlReader.Create(response.GetResponseStream());
XDocument xdoc = XDocument.Load(reader);
XNamespace xmlns = "http://purl.org/atom/ns#";

All the parsing can be done with a single LINQ to XML instruction, which iterates the entries and instantiates a new MailMessage object for each email, setting its Subject, Body and From properties with the corresponding values in the feed:

var messages = from entry in xdoc.Descendants(xmlns + "entry")
               from author in entry.Descendants(xmlns + "author")
               select new MailMessage() {
                   Subject = entry.Element(xmlns + "title").Value,
                   Body = entry.Element(xmlns + "summary").Value,
                   From = new MailAddress(
                       author.Element(xmlns + "email").Value,
                       author.Element(xmlns + "name").Value)
               };

At this point, messages will contain a collection of MailMessage instances that we can process or simply dump to the console as in the following snippet:

Console.WriteLine("Number of messages: " + messages.Count());
foreach (MailMessage entry in messages) {
    Console.WriteLine();
    Console.WriteLine("Subject: " + entry.Subject);
    Console.WriteLine("Summary: " + entry.Body);
    Console.WriteLine("Author: " + entry.From);
}

If you have any questions about how to use the Google Data APIs .NET Client Library to access the Gmail Inbox Feed, please post them in the client library discussion group.

Undo certain Gmail actions in your mobile browser

Sometimes when I’m using Gmail on my phone, I delete a message by mistake or label it incorrectly. Sure I can fish the message out of my Trash or remove the label and apply the correct one, but that takes several steps. Even just a few seconds is usually enough time to catch those annoying mistakes.

Now when you use the Gmail mobile web app, you’ll have a small window of opportunity to undo four key actions: archive, delete, add or remove a label, or move a message/conversation.

When you take one of these actions, Gmail displays a yellow bar that recaps what you just did and allows you to undo it:

This bar stays in position even if you move to another screen (e.g. moving to ‘Menu’ from ‘Inbox’). If you don’t happen to catch your mistake in time, not to worry: all four actions can still be undone through other means (e.g. you can move a message from Trash back into your Inbox).

Try it out at gmail.com in the browser of your Android or iOS device.

Gmail for Android: better Priority Inbox support and improved compose

When we first released Gmail in Android Market back in September, we said that you’d be getting new stuff faster, and we meant it. After getting thousands of comments on that release, we made a bunch of updates based on your feedback and today we’re launching Gmail for Android 2.3.2.

Priority Inbox

First of all, you told us that you love Priority Inbox and expect much better support for it on your phone. Now you can see important messages in a new Priority Inbox view.

This view includes all important messages in your inbox, regardless of whether you’ve read them or not. You can archive and delete conversations or mark them unimportant from there. You’ll notice the importance markers you’re used to seeing in the desktop version of Gmail, and you can also change a conversation’s importance using the menu. To switch between inboxes or labels, try tapping on the current label.

Ever wanted to know that you got an important message without taking your phone out of your pocket? Now you can set up your phone to notify, vibrate, or ring on just your new important mail (check out Menu > Settings > Priority Inbox).

While Priority Inbox on your Android phone doesn’t have all the features offered in the desktop version of Gmail, we think this is a good start and plan to add even more functionality moving forward.


Improved Compose

Since our last Market update, we adopted a few features related to composing messages from the desktop version of Gmail. Many of you asked for a better way to switch between replying to the sender to replying to all. Now, you can easily switch between reply, reply all, and forward while composing your response.

If you moved to Gmail from another webmail provider and want to continue to send email from that address, now you can send from any address you’ve configured in the desktop version of Gmail.

In addition, you can now respond to messages in-line.

You won’t need to wait for Gingerbread to get these updates. This version of the Gmail app works for Android 2.2 (Froyo) and newer releases in most countries. (Not sure if your device is running the right version? Check here.) Get the update from Android Market (just scan the QR code below, or click here if you’re on a phone) and check out the new Gmail. And don’t forget to send us your feedback from within the new version of the app (from your Inbox: Menu > More > About > Feedback).

Posted by Paul Westbrook, Gmail for Android team