How to show unread mail from gmail using asp.net


In this article I'm going to explain how to show unread gmail in asp.net. First you need open Visual studio 2005 and create a new web application by going to file > new Web site and then just to drag and drop two tex tbox and one button control your asp.net web page.

Gmail support the standard POP and IMAP Protocols for email receieve but sometimes you need only to know wheterh there are any new messages in your mail box. Currently Gmail provides a list of unread emails in a subscriber inbox as an atom feed.
Atom is a system which makes it easy for you to receive in one place , regular updates from news, website, blogs ,gamil etc.

1) Fire up Visual studio 2005 and create a new web application by going to file > new Web site.

2) Add a class file to the project (GmailHandlerClass).

3)Just drag and drop two textbox and one button control your asp.net web page.

4)Within button click event just call GmailHandlerClass.
5)If username and password is valid then you will get unread mail in your web page.


Design Page


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
User Name
<asp:TextBox ID="txtUsername" runat="server"></asp:TextBox><br />
Password
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox><br />
<asp:Button ID="btnSend" runat="server" OnClick="btnSend_Click" Text="Send" /></div>
</form>
</body>
</html>


Coding Page

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;


public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{


}
protected void btnSend_Click(object sender, EventArgs e)
{
GmailHandlerClass gmailFeedOutput = new GmailHandlerClass(txtUsername.Text.ToString(), txtPassword.Text.ToString());
XmlDocument myXmlvalue = gmailFeedOutput.GetGmailAtomToRead();
XmlNodeReader xmlReadervalue = new XmlNodeReader(myXmlvalue);
XmlElement root = myXmlvalue.DocumentElement;
XmlNodeList xmlnodes = myXmlvalue.GetElementsByTagName("entry");
for (int ij = 0; ij < xmlnodes.Count; ij++)
{
Response.Write("
");
Response.Write(xmlnodes[ij].FirstChild.Name);
Response.Write(":\t" + xmlnodes[ij].FirstChild.InnerText + " \n");
Response.Write(xmlnodes[ij].LastChild.Name);
Response.Write(":\t" + xmlnodes[ij].LastChild.InnerText + " \n");
Response.Write("\r\n\r\n");
Response.Write("
");
}
}
}



Class File

using System;
using System.Data;
using System.Xml;
using System.Net;
using System.IO;

///
/// Summary description for Class1
///

public class GmailHandlerClass
{
private string strusername;
private string strpassword;
private string strgmailAtomUrl;

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

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

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

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

public GmailHandlerClass(string _Username, string _Password)
{
Username = _Username;
Password = _Password;
GmailAtomUrl = "https://mail.google.com/mail/feed/atom";
}
public XmlDocument GetGmailAtomToRead()
{
byte[] buffers = new byte[8192];
int byteCounts = 0;
XmlDocument _feedXmls = null;
try
{
System.Text.StringBuilder strBuilder = new System.Text.StringBuilder();
WebRequest webRequest = WebRequest.Create(GmailAtomUrl);

webRequest.PreAuthenticate = true;

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

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

while ((byteCounts = stream.Read(buffers, 0, buffers.Length)) > 0)
strBuilder.Append(System.Text.Encoding.ASCII.GetString(buffers, 0, byteCounts));


_feedXmls = new XmlDocument();
_feedXmls.LoadXml(strBuilder.ToString());


}
catch (Exception ex)
{

throw ex;
}
return _feedXmls;
}
}


Comments



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: