Import Gmail contacts in Asp.net application

Its very simple to import contact from Gmail account. We need add four references in our application. like

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

Add name space on the top of the page:

  1. using Google.Contacts;
  2. using Google.GData.Client;
  3. using Google.GData.Contacts;
  4. using Google.GData.Extensions;

Code behind code:

  1.    <form id="form1" runat="server">
  2.    <div>
  3.    
  4.         <b>Email Address : </b>
  5.         <br />
  6.         <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
  7.         <br />
  8.         <br />
  9.         <b>Password : </b>
  10.         <br />
  11.         <asp:TextBox ID="txtPassword" runat="server" TabIndex="1" TextMode="Password"></asp:TextBox>
  12.         <br />
  13.         <br />
  14.         <asp:Button ID="btnContacts" runat="server" onclick="btnContacts_Click"
  15.             TabIndex="2" Text="Import Contacts" Width="125px" />
  16.         <br />
  17.         <br />
  18.         <br />
  19.         <b>Contacts:<br />
  20.         </b>
  21.         <asp:ListBox ID="lstContacts" runat="server" Height="176px" Width="229px">
  22.         </asp:ListBox>
  23.         <br />
  24.         <br />
  25.    
  26.     </div>
  27.     </form>

C# code:

  1. protected void btnContacts_Click(object sender, EventArgs e)
  2.     {
  3.         //Provide Login Information
  4.         RequestSettings rsLoginInfo = new RequestSettings("", txtEmail.Text, txtPassword.Text);
  5.         rsLoginInfo.AutoPaging = true;
  6.  
  7.         // Fetch contacts and dislay them in ListBox
  8.         ContactsRequest cRequest = new ContactsRequest(rsLoginInfo);
  9.         Feed<Contact> feedContacts = cRequest.GetContacts();
  10.         foreach (Contact gmailAddresses in feedContacts.Entries)
  11.         {
  12.             Console.WriteLine("\t" + gmailAddresses.Title);
  13.             lstContacts.Items.Add(gmailAddresses.Title);
  14.             foreach (EMail emailId in gmailAddresses.Emails)
  15.             {
  16.                 Console.WriteLine("\t" + emailId.Address);
  17.                 lstContacts.Items.Add(" " + emailId.Address);
  18.             }
  19.         }
  20.     }




facebooktwittergoogle_plusredditpinterestlinkedinmail