In this post,I will show you how to read mail from gmail in asp.net
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %> <!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 runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> UserName : <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox><br /> Password: <asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox><br /> <asp:Button ID="btnLogin" runat="server" Text="Login" OnClick="btnLogin_Click" /><br /> <asp:GridView ID="grdMail" runat="server"> </asp:GridView> </div> </form> </body> </html>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Net; using System.Text; using System.Xml.Linq; public partial class Default2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btnLogin_Click(object sender, EventArgs e) { XNamespace ns = "http://purl.org/atom/ns#"; string userName = txtUserName.Text; string password = txtPassword.Text; string gmailFeedUrl = @"https://mail.google.com/mail/feed/atom "; WebClient client = new WebClient(); string credential = string.Format("{0}:{1}", userName, password); string auth = "Basic" + Convert.ToBase64String(Encoding.ASCII.GetBytes(credential)); client.Headers.Add("Authorization", auth); var inboxXml = client.DownloadString(gmailFeedUrl); var xml = XDocument.Parse(inboxXml); var query = from entry in xml.Descendants(ns + "entry") select new { Title = entry.Element(ns + "title").Value, Author = entry.Element(ns + "author").Element(ns + "name").Value, }; grdMail.DataSource = query; grdMail.DataBind(); } }
No comments:
Post a Comment