Retrieve Gmail Contacts Name with Email in ASP.NET

Introduction

This blog I am going to describe how to retrieve gmail contact with Name and Mail id in asp net.  Here I will describe how to communicate with Gmail using Google API.

Description

As it is a third party software for this we required some third party dll to be referenced to our application which will communicate with Gmail server.

For this we have to download the Google API Set up from below link

http://code.google.com/p/google-gdata/

Or you can direct download from this link

https://google-gdata.googlecode.com/files/Google_Data_API_Setup_2.1.0.0.msi

Install this setup you will get these dll files in your Pc

  1. Google.GData.Apps.dll
  2. Google.GData.Client.dll
  3. Google.GData.Contacts.dll
  4. Google.GData.Extensions.dll

Collect these dll's.

Design

Now design your design the screen as below screen

index.jpg

Or Copy the source code below

<%@ 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 runat="server">
    <title></title>
</head>
<
body>
    <form id="form1" runat="server">
    <div>
        <table align="center" width="100%">
            <tr>
                <td>
                    <table align="center">
                    <tr></tr>
                        <tr>
                            <td>
                                <asp:Label ID="Label1" runat="server" Text="User name"></asp:Label>
                            </td>
                            <td>
                                <asp:TextBox ID="txtName" runat="server" Width="200px"></asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <asp:Label ID="Label2" runat="server" Text="Password"></asp:Label>
                            </td>
                            <td>
                                <asp:TextBox ID="txtPwd" runat="server" TextMode="Password" Width="200px"></asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                            <td colspan="2" align="center">
                                <asp:Button ID="btnLogin" runat="server" Text="Get Contact" OnClick="btnLogin_Click" />
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr>
                <td align="center">
                    <asp:GridView ID="gvContacts" runat="server" EmptyDataText="No contacts" AllowPaging="true"
                        Width="50%" PageSize="15" AutoGenerateColumns="false"                        OnPageIndexChanging="gvContacts_PageIndexChanging">
                        <Columns>
                            <asp:TemplateField HeaderText="Contact Name">
                                <ItemTemplate>
                                    <asp:Label ID="lblName" runat="server" Text='<%#Eval("Name") %>'></asp:Label>
                                </ItemTemplate>
                                <ItemStyle Width="200px" />
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Contact Email">
                                <ItemTemplate>
                                    <asp:Label ID="lblEmail" runat="server" Text='<%#Eval("EmailID") %>'></asp:Label>
                                </ItemTemplate>
                                <ItemStyle Width="100px" />
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</
html>

 Next add reference of Google gdata dll to your website.

  1. Google.GData.Apps.dll
  2. Google.GData.Client.dll
  3. Google.GData.Contacts.dll
  4. Google.GData.Extensions.dll

And write the below code .cs file

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Google.GData.Contacts;
using Google.GData.Client;
using Google.GData.Extensions;
using Google.Contacts;

 public partial class _Default : System.Web.UI.Page
{
 public DataSet GetGmailContacts(string AppName, string Uname, string UPassword)

    {
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();

        dt.Columns.Add(new DataColumn("EmailID", typeof(string)));
        dt.Columns.Add(new DataColumn("Name", typeof(string)));
        RequestSettings rs = new RequestSettings(AppName, Uname, UPassword);
        rs.AutoPaging = true;
        ContactsRequest cr = new ContactsRequest(rs);

        Feed<Contact> f = cr.GetContacts();
        foreach (Contact t in f.Entries)
        {
            foreach (EMail email in t.Emails)
            {
                DataRow dr1 = dt.NewRow();
                dr1["EmailID"] = email.Address.ToString();
                dr1["Name"] = t.Title.ToString();
                dt.Rows.Add(dr1);
            }
        }
        ds.Tables.Add(dt);
        return ds;
    }
     protected void Page_Load(object sender, EventArgs e)
    {
    }

     protected void btnLogin_Click(object sender, EventArgs e)
    {
        DataSet ds = GetGmailContacts("Gmail Contact Operations!", txtName.Text, txtPwd.Text);
        ViewState["Contact"] = ds;
        gvContacts.DataSource = ds;
        gvContacts.DataBind();
    }

    protected void gvContacts_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {

        DataSet ds = ViewState["Contact"] as DataSet;
        gvContacts.PageIndex = e.NewPageIndex;
        gvContacts.DataSource = ds;
        gvContacts.DataBind();
    }
}
 

Now build your Application. And enter Gmail id and password in the corresponding textbox

Click on Get Contact button it will show all your gmail Contact Name with Email.

index1.jpg

Any modification or problem Plz comment.

Ebook Download
View all
Learn
View all