Monday 18 March 2013

how to import Contacts from GMAIL using ASP.NET and C#

 

Description:-

            In this Example we explain that how to import or Fetch or Retrieve Contacts of Gmail of a user by using GContacts Data ApI. It is one kind of Functionality provide by Google and are used to Import Contacts of Gmail.

In this example we have developed a one social network site and in which we provide a Facility to user to Imports Its Contact from Gmail and Bind it to Gridview


To send SMS in Asp.Net to any User then click Here Send SMS or message in Asp.net

To Send Email in Asp.Net click Here Send Email to Other user in ASP.net

 To send Email with Attechment File click Here Send Email with Atteched File in Asp.Net

To Import Gmail Contact in Asp.Net click here Import contact from Gmail in asp.net

To send Email to Multiple User at a time based on checkbox selection click here Send Email to Multiple User in Asp.net

To Send Forgot to an Email in Asp.Net click here Send Forgot Password to user's Email
I used ASP.NET and C# for developing this application.
We need to follow below steps to get gmail contacts 

Step-1:  Download Google data API setup from the specified URL 

Or you can directly download with the following

That Set Up will installs set of Google Data dll’s (Google.GData.Apps.dll, Google.GData.Client.dll, Google.GData.Contacts.dll, Google.GData.Extensions.dll) into client installed Machine, you should collect that dll’s for your program.

Step-2:  Design our aspx page (Default.aspx) by using with the following UI as simple scenario.



<%@ 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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table border="1">
                <tr>
                    <td>Gmail User Name</td>
                    <td>
                        <asp:TextBox ID="txtunm" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>Gmail Password</td>
                    <td>
                        <asp:TextBox ID="txtpwd" runat="server" TextMode="Password"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td>
                        <asp:Button ID="Fetchdatafromgmail" runat="server" Text="Fetchdatafromgmail" OnClick="Fetchdatafromgmail_Click" />
                    </td>
                </tr>
            </table>
        </div>
        <div>
            <asp:GridView ID="gvgmaildata" runat="server" CellPadding="4" EnableModelValidation="True" ForeColor="#333333" GridLines="None">
                <AlternatingRowStyle BackColor="White" />
                <EditRowStyle BackColor="#2461BF" />
                <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                <RowStyle BackColor="#EFF3FB" />
                <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            </asp:GridView>
        </div>

    </form>
</body>
</html>

 Step-3:  Add those downloaded google dll’s as reference to your website in visual studio->Solution Explorer ->Right Click-> Click on Add Reference….->Browse ->Get dll’s from Installed Location->Press OK.

Step-4: Code Behind Code



using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Google.GData.Contacts;
using Google.GData.Client;
using Google.GData.Extensions;
using Google.Contacts;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public static DataSet GetGmailData(string App_Name, string GmailUserName, string GmailPassword)
    {

        DataSet dsd = new DataSet();

        DataTable dtd = new DataTable();

        DataColumn Clmn = new DataColumn();

        Clmn.DataType = Type.GetType("System.String");

        Clmn.ColumnName = "EmailID";

        dtd.Columns.Add(Clmn);



        RequestSettings rs = new RequestSettings(App_Name, GmailUserName, GmailPassword);

        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 rows = dtd.NewRow();

                rows["EmailID"] = email.Address.ToString();

                dtd.Rows.Add(rows);

            }

        }

        dsd.Tables.Add(dtd);

        return dsd;

    }
    protected void Fetchdatafromgmail_Click(object sender, EventArgs e)
    {

        DataSet ds = GetGmailData("MyNetwork Web Application!", txtunm.Text, txtpwd.Text);

        gvgmaildata.DataSource = ds;

        gvgmaildata.DataBind();
    }
}


0 comments:

Post a Comment