Read Gmail Inbox using Gmail Feed in asp.net........................

Unknown | 6:36 AM |

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");
        }

    }
}


Binding Dropdownlist with full path like in Treeview.................

 <asp:DropDownList ID="ddclient" runat="server" Width="125px" >
                                        </asp:DropDownList>


  <table>
                    <tr>
                        <td valign="top" align="left">
                            <asp:TreeView ID="treeClient" runat="server" Visible="False">
                            </asp:TreeView>
                        </td>
                        <td valign="top" align="left">
                            <asp:DropDownList ID="DropDownListTree" runat="server" Visible="False">
                            </asp:DropDownList>
                        </td>
                    </tr>
                </table>



Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
       
                If Not Page.IsPostBack Then
                    PopulateRootLevel()
                    AddNoteToCombo(treeClient.Nodes, "")
                    fillOrg4client()
                End If
        Catch ex As Exception
     
        End Try
    End Sub



Private Sub fillOrg4client()
        Dim rw As New ListItem
        ddclient.Items.Clear()

        For Each lis As ListItem In DropDownListTree.Items
            Dim sr = Replace(lis.Text, "%o", "")
            sr = Replace(sr, "%p", "")
            'If sr.Contains(cmbClient.SelectedItem.Text) Then
            '    If cmbClient.SelectedItem.Text <> sr Then
            rw = New ListItem
            rw.Text = sr
            rw.Value = lis.Value
            ddclient.Items.Add(rw)
            '    End If

            'End If
        Next
        'End If

        rw = New ListItem()
        rw.Text = "Select"
        rw.Value = -1
        ddclient.Items.Insert(0, rw)

        ddclient.SelectedValue = -1
    End Sub




Private Sub treeClient_TreeNodePopulate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.TreeNodeEventArgs) Handles treeClient.TreeNodePopulate
        PopulateSubLevel(CInt(e.Node.Value), e.Node)
    End Sub
    Private Sub AddNoteToCombo(ByVal nc As TreeNodeCollection, ByVal str As String)
        For i As Integer = nc.Count - 1 To 0 Step -1
            Dim lst As New ListItem
            lst.Value = nc(i).Value
            str = Replace(str, "Surgere%o", "", 1, 9) 'Remove this to add surge to all
            If str = "" Then
                lst.Text = nc(i).Text
            Else

                lst.Text = str + "\" + nc(i).Text
            End If
            If Not lst.Text.Equals("Surgere%o") Then
                DropDownListTree.Items.Add(lst)
            End If

            If nc(i).ChildNodes.Count > 0 Then
                If str = "" Then
                    AddNoteToCombo(nc(i).ChildNodes, nc(i).Text)
                Else

                    AddNoteToCombo(nc(i).ChildNodes, str + "\" + nc(i).Text)
                End If

            End If
            'If nc(i).ChildNodes.Count = 0 Then

            ' End If
        Next
    End Sub

    Private Sub PopulateRootLevel()
        DropDownListTree.Items.Clear()
        Dim stBuilder As New StringBuilder()
        stBuilder.Append("select CLIENT_ID,CLIENT_NAME,o_or_p,active_yn,(select count(*) FROM client ")
        stBuilder.Append(" WHERE reports_to_id=sc.CLIENT_ID) childnodecount FROM client sc where reports_to_id=0")
        Dim selectQry = stBuilder.ToString()
        'Dim selectQry = "select CLIENT_ID,CLIENT_NAME,o_or_p,active_yn,(select count(*) FROM client " &
        '           " WHERE reports_to_id=sc.CLIENT_ID) childnodecount FROM client sc where reports_to_id=0"
        'Dim cmm As SqlCommand = New SqlCommand(selectQry)
        'Dim result As Object
        Dim dtTree = New DataTable
        dtTree = DataConnect.GetInstance.GetDt(selectQry)

        PopulateNodes(dtTree, treeClient.Nodes, "")
        treeClient.ExpandAll()
    End Sub
    Private Sub PopulateNodes(ByVal dt As DataTable, ByVal nodes As TreeNodeCollection, ByVal st As String)
        For Each dr As DataRow In dt.Rows

            Dim tn As New TreeNode()

            tn.Text = dr("client_name").ToString() ' + "<asp:ImageButton runat='server' ImageUrl='~/Images/plus.gif' onClick='clickPlus()'>"
            tn.Value = dr("client_id").ToString()
            If dr("o_or_p").ToString().Trim() = "p" Then
                tn.Text = tn.Text + "%p"


            Else
                tn.Text = tn.Text + "%o"

            End If
            tn.Target = dr("client_name").ToString()
            nodes.Add(tn)
            'DropDownListTree.Items.Add(st + ">" + tn.Text)
            'If node has child nodes, then enable on-demand populating
            tn.PopulateOnDemand = (CInt(dr("childnodecount")) > 0)
        Next
    End Sub
    Private Sub PopulateSubLevel(ByVal parentid As Integer, ByVal parentNode As TreeNode)

        Dim stBuilder As New StringBuilder()
        stBuilder.Append("select CLIENT_ID,CLIENT_NAME,o_or_p,active_yn,(select count(*) FROM client ")
        stBuilder.Append(" WHERE reports_to_id=sc.CLIENT_ID) childnodecount FROM client sc where reports_to_id=@parentID")
        Dim selectQry = stBuilder.ToString()
        'Dim selectQry = "select CLIENT_ID,CLIENT_NAME,o_or_p,active_yn,(select count(*) FROM client " &
        '      " WHERE reports_to_id=sc.CLIENT_ID) childnodecount FROM client sc where reports_to_id=@parentID"

        Dim objCommand = New SqlCommand(selectQry)
        objCommand.Parameters.Add("@parentID", SqlDbType.Int).Value = parentid
        Dim dt = New DataTable
        dt = DataConnect.GetInstance.GetDt(objCommand)
        PopulateNodes(dt, parentNode.ChildNodes, "")
    End Sub

Count Login Atempts...............................

CountLoginAtempts()
                        label_failed_authentication.Text = "The username or password you entered is incorrect or not found."
                        label_failed_authentication.Visible = True



Private Sub CountLoginAtempts()
        Dim result As Object
        Dim count As Integer
        count = 1
        Dim cmm = New SqlCommand("select account_bad_attempts from users where email='" & text_Username.Text & "'")
        result = DataConnect.GetInstance.ExecuteScalar(cmm)
        If result IsNot Nothing Then
            count = count + Convert.ToInt64(result)
            If count < 5 Then
                cmm = New SqlCommand("Update users set account_bad_attempts=@count where email='" & text_Username.Text & "'")
            Else
                cmm = New SqlCommand("Update users set account_bad_attempts=@count,account_locked_yn=1 where email='" & text_Username.Text & "'")
            End If
            cmm.Parameters.Add("@count", SqlDbType.BigInt, 0).Value = count
            DataConnect.GetInstance.ExecuteNonQuery(cmm)
        End If
    End Sub

Category:

About http://dotnetvisual.blogspot.in/:
DOT NET TO ASP.NET is a web application framework marketed by Microsoft that programmers can use to build dynamic web sites, web applications and web services. It is part of Microsoft's .NET platform and is the successor to Microsoft's Active Server Pages (ASP) technology. ASP.NET is built on the Common Language Runtime, allowing programmers to write ASP.NET code using any Microsoft .NET language. create an application very easily ....