Hardware Gmail Notifier – C# code

July 11, 2008 at 8:20 pm | Posted in MSP430, My Projects | 1 Comment
Tags: ,

Here is the code that I used to get the number of email from Gmail in C#.

/// <summary>
/// Get the Number of Email
/// </summary>
/// <param name=”userName”></param>
/// <param name=”passWord”></param>
/// <returns></returns>
private int GetNumberOfMail(string userName, string passWord)
{
XmlDocument xmldoc = GetGmailFeed(userName, passWord);
XmlNodeList count = xmldoc.GetElementsByTagName(“fullcount”);
return int.Parse(count[0].InnerText);
}
/// <summary>
/// Get the Gmail Atom Feed
/// Adapted from Kevin Daly
/// http://www.dotnetjunkies.com/WebLog/kevdaly/archive/2004/12/24/38612.aspx
/// </summary>
/// <param name=”userName”></param>
/// <param name=”password”></param>
/// <returns></returns>
private static XmlDocument GetGmailFeed(string userName, string password)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(“https://mail.google.com/mail/feed/atom/&#8221;);
req.Method = “GET”;
req.Credentials = new NetworkCredential(userName, password);
XmlDocument response = new XmlDocument();
HttpWebResponse resp;
try
{
resp = (HttpWebResponse)req.GetResponse();
if (resp.StatusCode == HttpStatusCode.OK)
{
XmlTextReader reader = new XmlTextReader(resp.GetResponseStream());
response.Load(reader);
reader.Close();
}
resp.Close();
}
catch Console.WriteLine(“Error… HttpWebResponse”);
return response;
}

Note: I do not guarantee this code will always work. Use it at your own risk.

1 Comment »

RSS feed for comments on this post. TrackBack URI

  1. If you could, send me a email, when you post the code here.

    thiago dot farina @ gmail dot com

    I made a variant of GetGmailFeed( ) and is working to me.

    Thanks!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.
Entries and comments feeds.

%d bloggers like this: