Import Contacts From Gmail by Using ASP.Net and C#

For getting contacts from Gmail, first we need Google data DLLs, which can be downloaded from here.

Download the installer and install, this installer will install the Google data API.

After that add the reference of these DLLs into your project and include the namespace into your class.

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

After that you need to create RequestSettings and ContactsRequest

So first we create the object of RequestSettings and pass that object to ContactsRequest as in the following:

  1. RequestSettings _requestSettings = new RequestSettings("applicationName""GmailUserID""GmailPassword");  
  2. ContactsRequest _contactsRequest = new ContactsRequest(_requestSettings);  
After that we will call the GetContacts method as in the following:

  1. Feed<Contact> ContactList = _contactsRequest.GetContacts();  

The preceding method will return a list of contacts.

For demo purposes, I have created a aspx page that will ask for your Gmail userid and password and when you enter valid credentials, it displays all the friend's Email.

Code for aspx page:

  1. <form id="form1" runat="server">  
  2.     <div>  
  3.         <table>  
  4.             <tr>  
  5.                 <td colspan="2">  
  6.                     <asp:Label ID="LabelErrorMessage" runat="server" ForeColor="Red" Visible="false"></asp:Label>  
  7.                 </td>  
  8.             </tr>  
  9.             <tr>  
  10.                 <td>  
  11.                     <strong>Enter userid: </strong>  
  12.                 </td>  
  13.                 <td>  
  14.                     <asp:TextBox ID="TextBoxUserID" runat="server"></asp:TextBox>  
  15.                 </td>  
  16.             </tr>  
  17.             <tr>  
  18.                 <td>  
  19.                     <strong>Enter Password: </strong>  
  20.                 </td>  
  21.                 <td>  
  22.                     <asp:TextBox ID="TextBoxPassword" runat="server" TextMode="Password"></asp:TextBox>  
  23.                 </td>  
  24.             </tr>  
  25.             <tr>  
  26.                 <td>  
  27.                 </td>  
  28.                 <td>  
  29.                     <asp:Button ID="ButtonImportContacts" runat="server" Text="Import Contacts" OnClick="ButtonImportContacts_Click" />  
  30.                 </td>  
  31.             </tr>  
  32.         </table>  
  33.     </div>  
  34.     <div>  
  35.         <asp:Repeater ID="RepeaterContactsList" runat="server">  
  36.             <HeaderTemplate>  
  37.                 <b>Friends EmailID</b>  
  38.             </HeaderTemplate>  
  39.             <ItemTemplate>  
  40.                 <br />  
  41.                 <%# DataBinder.Eval(Container.DataItem, "EmailID")%>  
  42.             </ItemTemplate>  
  43.         </asp:Repeater>  
  44.     </div>  
  45. </form>  

Code for .cs page

 

  1. using System;  
  2. using System.Configuration;  
  3. using System.Data;  
  4. using System.Web;  
  5. using System.Web.Security;  
  6. using System.Web.UI;  
  7. using System.Web.UI.HtmlControls;  
  8. using System.Web.UI.WebControls;  
  9. using System.Collections.Generic;  
  10. using Google.GData.Contacts;  
  11. using Google.GData.Client;  
  12. using Google.GData.Extensions;  
  13. using Google.Contacts;  
  14.   
  15. public partial class _Default : System.Web.UI.Page  
  16. {  
  17.     protected void Page_Load(object sender, EventArgs e)  
  18.     {  
  19.   
  20.     }  
  21.   
  22.     public List<GmailContacts> ImportContacts(string applicationName, string userName, string userPassword)  
  23.     {  
  24.         List<GmailContacts> emailList = new List<GmailContacts>();  
  25.         RequestSettings _requestSettings = new RequestSettings(applicationName, userName, userPassword);  
  26.         _requestSettings.AutoPaging = true;  
  27.         ContactsRequest _contactsRequest = new ContactsRequest(_requestSettings);  
  28.         Feed<Contact> ContactList = _contactsRequest.GetContacts();  
  29.         try  
  30.         {  
  31.             LabelErrorMessage.Visible = false;  
  32.             foreach (Contact contact in ContactList.Entries)  
  33.             {  
  34.                 foreach (EMail email in contact.Emails)  
  35.                 {  
  36.                     GmailContacts gc = new GmailContacts();  
  37.                     gc.EmailID = email.Address;  
  38.                     emailList.Add(gc);  
  39.                 }  
  40.             }  
  41.   
  42.         }  
  43.   
  44.         catch (Exception ex)  
  45.         {  
  46.             LabelErrorMessage.Visible = true;  
  47.             LabelErrorMessage.Text = ex.Message;  
  48.         }  
  49.   
  50.         return emailList;  
  51.   
  52.     }  
  53.   
  54.     protected void ButtonImportContacts_Click(object sender, EventArgs e)  
  55.     {  
  56.         List<GmailContacts> ImportList = new List<GmailContacts>();  
  57.         ImportList = ImportContacts("ImportContacts", TextBoxUserID.Text, TextBoxPassword.Text);  
  58.         RepeaterContactsList.DataSource = ImportList;  
  59.         RepeaterContactsList.DataBind();  
  60.     }  
  61. }  
  62.   
  63. public class GmailContacts  
  64. {  
  65.     private string _EmailID;  
  66.    
  67.   
  68.     public string EmailID  
  69.     {  
  70.         get  
  71.         {  
  72.             return _EmailID;  
  73.          }  
  74.   
  75.         set  
  76.         {  
  77.             _EmailID = value;  
  78.         }  
  79.     }  
  80. }  

 

By this way you can easily import Gmail contacts.


Similar Articles