Click here to Skip to main content
15,886,362 members
Articles / Web Development / HTML

.NET Interop for Gadgets – A C# GMail Inbox Reader Example

Rate me:
Please Sign up or sign in to vote.
4.84/5 (108 votes)
30 Jan 2007CPOL9 min read 654.6K   61.4K   259  
How to call absolutely any .NET code from your Vista Sidebar Gadget
using System;
using System.Collections;
using System.IO;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;
using System.Xml.Serialization;

namespace GmailReader
{
    /// <summary>
    /// A simple class to consume the mail feed for any GMail account
    /// </summary>
    [ComVisible(true)]
    public class GmailClient : IDisposable
    {
        private const string GmailUri = "https://mail.google.com/mail/feed/atom";
        private string _userName;
        private string _password;
        private GmailList _newMailList;

        public GmailClient(string userName, string password)
        {
            _userName = userName;
            _password = password;
        }
        /// <summary>
        /// I'd prefer to return the generic list here instead of using the GetMailItem 
        /// method to get individual items, but javascript doesn't play nice with generics.
        /// </summary>
        public void GetUnreadMail()
        {
            try 
            {
                // Get the XML feed from mail.google.com
                XmlElement element = GetFeedXml();

                if (element != null)
                {
                    // Deserialize the transformed XML into a generic list of GmailItem objects
                    XmlNodeReader reader = new XmlNodeReader(element);
                    XmlSerializer serializer = new XmlSerializer(typeof(GmailList));

                    _newMailList = serializer.Deserialize(reader) as GmailList;
                }
            }
            catch { }
        }
        /// <summary>
        /// The number of items in the unread mail collection
        /// </summary>
        public object UnreadMailCount
        {
            get 
            {
                if (_newMailList != null)
                {
                    return _newMailList.Count;
                }
                else 
                {
                    return 0;
                }
            }
        }
        /// <summary>
        /// Returns the GmailItem at the specified index
        /// </summary>
        /// <param name="index">Index if the mail item to return</param>
        public GmailItem GetMailItem(int index)
        {
            if (_newMailList == null || index < 0 || index > _newMailList.Count)
            {
                throw new IndexOutOfRangeException();
            }

            return _newMailList[index];
        }
        /// <summary>
        /// Get the XML feed from google and transform it into a deserializable format
        /// </summary>
        private XmlElement GetFeedXml()
        {
            try
            {
                // Create a web request to get the xml feed
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(GmailUri);
                request.Method = "GET";
                request.Credentials = new NetworkCredential(_userName, _password);

                XmlDocument xml = null;
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                // If the request/response is successful
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    // Get the response stream containing the xml
                    using (XmlTextReader reader = new XmlTextReader(response.GetResponseStream()))
                    {
                        // Load the XSLT document (it's an embedded resource)
                        byte[] data = Encoding.ASCII.GetBytes(GmailReader.Properties.Resources.GmailTransform);

                        using (MemoryStream xsltStream = new MemoryStream(data))
                        {
                            // Create a text reader with the XSLT document
                            XmlTextReader stylesheetReader = new XmlTextReader(xsltStream);

                            XslCompiledTransform transform = new XslCompiledTransform();
                            transform.Load(stylesheetReader);

                            // Run an XSLT transform on the google feed to get an xml structure 
                            // that can be deserialized into a GmailList object
                            using (MemoryStream ms = new MemoryStream())
                            {
                                transform.Transform(new XPathDocument(reader), null, ms);
                                ms.Seek(0, SeekOrigin.Begin);

                                xml = new XmlDocument();
                                // Load the transformed xml
                                xml.Load(ms);
                            }
                        }
                    }
                }

                response.Close();

                return xml.DocumentElement;
            }
            catch
            {
            }

            return null;
        }

        #region IDisposable Members

        public void Dispose()
        {
            // Nothing to do here.
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer PageLabs
United States United States
I'm the founder of PageLabs, a web-based performance and SEO optimization site.

Give your site a boost in performance, even take a free speed test!

http://www.pagelabs.com

Comments and Discussions