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

Introduction:
In this article I will explain how to import gmail contacts of a user by using GContacts Data API provided by Google.
Description:


We have developed one social network site at that time we think that how to popularize the our social network site I have checked some of social network sites at that time we got idea to implement import contacts concept is best to intimate all of our friends at a time I have searched so many websites to implement this concepts but no result finally I have implemented import contacts from gmail 
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 
https://code.google.com/p/google-gdata/
Or you can directly download with the following
http://google-gdata.googlecode.com/files/Google%20Data%20API%20Setup%281.4.0.2%29.msi
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.
   1:  
   2: <html xmlns="http://www.w3.org/1999/xhtml">
   3: <head runat="server">
   4: <title>IMport Gmail Contacts</title>
   5: </head>
   6: <body>
   7: <form id="form1" runat="server">
   8: <div>
   9: <table>
  10: <tr>
  11: <td>
  12: UserName</td>
  13: <td>
  14: <asp:TextBox ID="txtgmailusername" runat="server"></asp:TextBox>
  15: </td>
  16: </tr>
  17: <tr>
  18: <td>
  19: Password</td>
  20: <td>
  21: <asp:TextBox ID="txtpassword" runat="server" 
  22: TextMode="Password"></asp:TextBox>
  23: </td>
  24: </tr>
  25: <tr>
  26: <td>
  27: </td>
  28: <td>
  29: <asp:Button ID="Button1" runat="server" Text="Button" 
  30: onclick="Button1_Click" />
  31: </td>
  32: </tr>
  33: </table>
  34: </div>
  35: <div>
  36: <asp:GridView ID="gvmails" runat="server"></asp:GridView>
  37: </div>
  38: </form>
  39: </body>
  40: </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:
A) Add namespace these namespace to your code behind
   1:  
   2: using Google.GData.Contacts;
   3: using Google.GData.Client;
   4: using Google.GData.Extensions;
   5: using Google.Contacts;
B) After that write following code in code behind

   1:  
   2: public static DataSet GetGmailContacts(string App_Name, string Uname, string 
   3: UPassword)
   4: {
   5: DataSet ds = new DataSet();
   6: DataTable dt = new DataTable();
   7: DataColumn C2 = new DataColumn();
   8: C2.DataType = Type.GetType("System.String");
   9: C2.ColumnName = "EmailID";
  10: dt.Columns.Add(C2);
  11: RequestSettings rs = new RequestSettings(App_Name, Uname, UPassword);
  12: rs.AutoPaging = true;
  13: ContactsRequest cr = new ContactsRequest(rs);
  14: Feed<Contact> f = cr.GetContacts();
  15: foreach (Contact t in f.Entries)
  16: {
  17: foreach (EMail email in t.Emails)
  18: {
  19: DataRow dr1 = dt.NewRow();
  20: dr1["EmailID"] = email.Address.ToString();
  21: dt.Rows.Add(dr1);
  22: }
  23: }
  24: ds.Tables.Add(dt);
  25: return ds;
  26: }
  27: protected void Button1_Click(object sender, EventArgs e)
  28: {
  29: DataSet ds = GetGmailContacts("MyNetwork Web Application!", 
  30: txtgmailusername.Text, txtpassword.Text);
  31: gvmails.DataSource = ds;
  32: gvmails.DataBind();
  33: }

No comments:

Post a Comment