How to import contacts from Gmail using ASP.NET

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.
  1. <html></html>  
  2. <body>  
  3.     <form id="form1">  
  4.         <div>  
  5.             <table>  
  6.                 <tr>  
  7.                     <td>UserName</td>  
  8.                     <td>  
  9.                         <asp:TextBox ID="txtgmailusername" runat="server"></asp:TextBox>  
  10.                     </td>  
  11.                 </tr>  
  12.                 <tr>  
  13.                     <td>Password</td>  
  14.                     <td>  
  15.                         <asp:TextBox ID="txtpassword" runat="server"TextMode="Password"></asp:TextBox>  
  16.                     </td>  
  17.                 </tr>  
  18.                 <tr>  
  19.                     <td></td>  
  20.                     <td>  
  21.                         <asp:Button ID="Button1" runat="server" Text="Button"onclick="Button1_Click" />  
  22.                     </td>  
  23.                 </tr>  
  24.             </table>  
  25.         </div>  
  26.         <div>  
  27.             <asp:GridView ID="gvmails" runat="server"></asp:GridView>  
  28.         </div>  
  29.     </form>  
  30. </body>  
  31.       
  32. </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:
 
Add namespace these namespace to your code behind
 
C#
  1. using Google.GData.Contacts;  
  2. using Google.GData.Client;  
  3. using Google.GData.Extensions;  
  4. using Google.Contacts;  
VB
  1. Imports Google.GData.Contacts  
  2. Imports Google.GData.Client  
  3. Imports Google.GData.Extensions  
  4. Imports Google.Contacts  
  5. Imports System.Data  
After that write following code in code behind
 
C#
  1. public static DataSet GetGmailContacts(string App_Name, string Uname, string UPassword)   
  2. {  
  3.     DataSet ds = new DataSet();  
  4.     DataTable dt = new DataTable();  
  5.     DataColumn C2 = new DataColumn();  
  6.     C2.DataType = Type.GetType("System.String");  
  7.     C2.ColumnName = "EmailID";  
  8.     dt.Columns.Add(C2);  
  9.     RequestSettings rs = new RequestSettings(App_Name, Uname, UPassword);  
  10.     rs.AutoPaging = true;  
  11.     ContactsRequest cr = new ContactsRequest(rs);  
  12.     Feed < Contact > f = cr.GetContacts();  
  13.     foreach(Contact t in f.Entries)   
  14.     {  
  15.         foreach(EMail email in t.Emails)   
  16.         {  
  17.             DataRow dr1 = dt.NewRow();  
  18.             dr1["EmailID"] = email.Address.ToString();  
  19.             dt.Rows.Add(dr1);  
  20.         }  
  21.     }  
  22.     ds.Tables.Add(dt);  
  23.     return ds;  
  24. }  
  25. Protected void Button1_Click(object sender, EventArgs e)   
  26. {  
  27.     DataSet ds = GetGmailContacts("MyNetwork Web Application!", txtgmailusername.Text, txtpassword.Text);  
  28.     gvmails.DataSource = ds;  
  29.     gvmails.DataBind();  
  30. }  
VB
  1. Public Shared Function GetGmailContacts(App_Name AsString, Uname As String, UPassword As String) As DataSet  
  2. Dim ds As New DataSet()  
  3. Dim dt As New DataTable()  
  4. Dim C2 As New DataColumn()  
  5. C2.DataType = Type.[GetType]("System.String")  
  6. C2.ColumnName = "EmailID"  
  7. dt.Columns.Add(C2)  
  8. Dim rs As New RequestSettings(App_Name, Uname, UPassword)  
  9. rs.AutoPaging = True  
  10. Dim cr As New ContactsRequest(rs)  
  11. Dim f As Feed(Of Contact) = cr.GetContacts()  
  12. For Each t As Contact In f.Entries  
  13. For Each email As EMail In t.Emails  
  14. Dim dr1 As DataRow = dt.NewRow()  
  15. dr1("EmailID") = email.Address.ToString()  
  16. dt.Rows.Add(dr1)  
  17. Next  
  18. Next  
  19. ds.Tables.Add(dt)  
  20. Return ds  
  21. End Function  
  22. Protected Sub Button1_Click(sender As Object, e AsSystem.EventArgs) Handles Button1.Click  
  23. Dim ds As DataSet = GetGmailContacts("MyNetwork Web Application!", txtgmailusername.Text, txtpassword.Text)  
  24. gvmails.DataSource = ds  
  25. gvmails.DataBind()  
  26. End Sub