Friday, April 27, 2012

Read Gmail Inbox using Gmail Feed in asp.net

GmailHandler.cs



using System;
using System.Data;
using System.Xml;
using System.Net;
using System.IO;
/*
 * this code made by Ahmed Essawy
 * AhmedEssawy@gmail.com
 * http://fci-h.blogspot.com
 */
/// <summary>
/// Summary description for Class1
/// </summary>
public class GmailHandler
{
    private string username;
    private string password;
    private string gmailAtomUrl;

    public string GmailAtomUrl
    {
        get { return gmailAtomUrl; }
        set { gmailAtomUrl = value; }
    }

    public string Password
    {
        get { return password; }
        set { password = value; }
    }

    public string Username
    {
        get { return username; }
        set { username = value; }
    }

    public GmailHandler(string _Username, string _Password, string _GmailAtomUrl)
    {
        Username = _Username;
        Password = _Password;
        GmailAtomUrl = _GmailAtomUrl;
    }

    public GmailHandler(string _Username, string _Password)
    {
        Username = _Username;
        Password = _Password;
        GmailAtomUrl = "https://mail.google.com/mail/feed/atom";
    }
    public XmlDocument GetGmailAtom()
    {
        byte[] buffer = new byte[8192];
        int byteCount = 0;
        XmlDocument _feedXml = null;
        try
        {
            System.Text.StringBuilder sBuilder = new System.Text.StringBuilder();
            WebRequest webRequest = WebRequest.Create(GmailAtomUrl);

            webRequest.PreAuthenticate = true;

            System.Net.NetworkCredential credentials = new NetworkCredential(this.Username, this.Password);
            webRequest.Credentials = credentials;

            WebResponse webResponse = webRequest.GetResponse();
            Stream stream = webResponse.GetResponseStream();

            while ((byteCount = stream.Read(buffer, 0, buffer.Length)) > 0)
                sBuilder.Append(System.Text.Encoding.ASCII.GetString(buffer, 0, byteCount));


            _feedXml = new XmlDocument();
            _feedXml.LoadXml(sBuilder.ToString());


        }
        catch (Exception ex)
        {
            //add error handling
            throw ex;
        }
        return _feedXml;
    }
}


Defult.aspx.cs





using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Xml;
using System.Net;
using System.IO;
using System.Net.Sockets;
using System.Net.Security;

/*
 * this code made by Ahmed Essawy
 * AhmedEssawy@gmail.com
 * http://fci-h.blogspot.com
 */
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //just 2 steps to get your Gmail Atom

        //1- Create the object from GmailHandler class
        GmailHandler gmailFeed = new GmailHandler("xxxx@gmail.com", "xxxxx");
        //2-get the feed :) ,Congratulations
        XmlDocument myXml = gmailFeed.GetGmailAtom();

        //this part of code is not necessary
        //this part should custom according to your
        //business needs
        XmlElement root = myXml.DocumentElement;
        XmlNodeList xmlnode = myXml.GetElementsByTagName("entry");
        for (int i = 0; i < xmlnode.Count; i++)
        {
            Response.Write(xmlnode[i].FirstChild.Name);
            Response.Write(":\t" + xmlnode[i].FirstChild.InnerText + " \n");
            Response.Write(xmlnode[i].LastChild.Name);
            Response.Write(":\t" + xmlnode[i].LastChild.InnerText + " \n");
            Response.Write("\r\n\r\n");
        }

    }
}


2 comments:

Unknown said...

Thanks! I didn't have a full handle on the WebRequest(). Great example

Unknown said...

Thanks! I didn't have a full handle on what the WebRequest() did. Great example.

Using Authorization with Swagger in ASP.NET Core

 Create Solution like below LoginModel.cs using System.ComponentModel.DataAnnotations; namespace UsingAuthorizationWithSwagger.Models {     ...