Getting Gmail Unread Email Through Atom Feed in ASP.NET

Introduction

Atom makes it easy to receive regular updates from news websites, blogs, and/or Gmail. This feed is only available for Gmail accounts on Google Apps domains.

To get a Gmail Feed, you can access the Atom Feed for your Gmail account on the following URL:

https://mail.google.com/mail/feed/atom

Enter your Gmail account credentials and access the Atom Feed for all unread Email.

Note: Gmail messages will appear in your aggregator only if there are unread messages in your account inbox.

The following C# code shows how to read/save the atom feed of that account after a valid authentication process:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Security.Principal;  
  8. using System.Net;  
  9. using System.Collections;  
  10. using System.IO;  
  11. using System.Xml;  
  12. using System.Data;   
  13.   
  14. public partial class _Default : System.Web.UI.Page  
  15. {  
  16.      
  17.     protected void Page_Load(object sender, EventArgs e)  
  18.     {  
  19.     }     
  20.     protected void showdet(object sender, EventArgs e)  
  21.     {         
  22.         NewMethod();  
  23.         login.Visible = false;  
  24.     }  
  25.   
  26.     private void NewMethod()  
  27.     {  
  28.         var Url = @"https://mail.google.com/mail/feed/atom";  
  29.         var username = TextBox1.Text.Trim();  
  30.         var password = TextBox2.Text.Trim();  
  31.         var encoded = TextToBase64(username + ":" + password);  
  32.         var request = HttpWebRequest.Create(Url);  
  33.         request.Method = "POST";  
  34.          request.ContentLength = 0;  
  35.          request.Headers.Add("Authorization""Basic" + encoded);  
  36.         var respone = request.GetResponse();  
  37.         var stream = respone.GetResponseStream();  
  38.         XmlReader reader = XmlReader.Create(stream);  
  39.         XmlDocument doc = new XmlDocument();  
  40.         doc.Load(reader);  
  41.          doc.Save(Server.MapPath("GmailFeed.xml"));  
  42.         DataSet dt = new DataSet();  
  43.         // dt.ReadXmlSchema(Server.MapPath("GmailFeed.xml"));  
  44.          dt.ReadXml(Server.MapPath("GmailFeed.xml"));        
  45.         DataTable DT = dt.Tables[3];        
  46.        // DT.Merge(dt.Tables[1]);  
  47.          DT.Merge(dt.Tables[2]);         
  48.         grid.DataSource = DT;  
  49.         DataBind();  
  50.         Session["Table"] = DT;  
  51.     }  
  52.   
  53.     public static string TextToBase64(string S)  
  54.     {  
  55.         System.Text.ASCIIEncoding encode = new System.Text.ASCIIEncoding();  
  56.         byte[] bytes = encode.GetBytes(S);  
  57.         return System.Convert.ToBase64String(bytes, 0, bytes.Length);  
  58.     }  
  59.   
  60.     protected void IndexChagning(object sender, GridViewPageEventArgs e)  
  61.     {  
  62.         grid.PageIndex = e.NewPageIndex;  
  63.         if (!object.Equals(Session["Table"], null))  
  64.         {  
  65.              grid.DataSource = Session["Table"as DataTable;  
  66.             DataBind();  
  67.         }                   
  68.     }  
  69. }  

Output

Enter Email and Password for your account and click on the button.

Gmail-Atom-Feed-In-Asp.net.png



You will see that all the new or unread message from your account inbox will appear.

Atom-Feed-of-gmail-account-in-asp.net.png


Similar Articles