Import Gmail Contacts In ASP.NET

In this article, learn how to import Gmail Contacts in ASP.NET using C#.

Introduction
 
Our clients often need to import their Gmail account emails into their application and send them emails. For example, a newsletter or product updates. 
 
Requirement
 
We need to use Google API to connect with Gmail and import emais. 
 
This link will download the following DLLs to your PC:
 
Google.GData.Apps.dll
Google.GData.Client.dll
Google.GData.Contacts.dll
Google.GData.Extensions.dll
 
Create a new Web Application and add reference to these DLLs to your project.
 
In the Solution Explorer, right-click on the "References" node then select "Add Reference...". In the "Add Reference" window select the "Browse" tab. Browse to the location where the DLLS were put and select one of them then click "OK". Do the same for the other three.
 
Source Code
 
Add the following source code to create a design like I created: 
  1. <html xmlns="http://www.w3.org/1999/xhtml">  
  2. <head id="Head1" runat="server">  
  3. <title>Import gmail contacts in to ASP.Net | Neha Sharma</title>  
  4. <style type="text/css">  
  5. .auto-style1 {  
  6. height: 26px;  
  7. }  
  8. </style>  
  9. </head>  
  10. <body>  
  11. <form id="form1" runat="server">  
  12. <div>  
  13. <table align="center" style="height: 104px; width: 301px; background-color: #F0F0F0">  
  14. <tr>  
  15. <td>  
  16. Gmail ID</td>  
  17. <td>  
  18. <asp:TextBox ID="txtgmailusername" runat="server"></asp:TextBox>  
  19. </td>  
  20. </tr>  
  21. <tr>  
  22. <td class="auto-style1">  
  23. Password</td>  
  24. <td class="auto-style1">  
  25. <asp:TextBox ID="txtpassword" runat="server" TextMode="Password"></asp:TextBox>  
  26. </td>  
  27. </tr>  
  28. <tr>  
  29. <td>  
  30. <asp:Button ID="Button1" runat="server" Text="Get" OnClick="Button1_Click" />  
  31. </td>  
  32. <td>  
  33.  </td>  
  34. </tr>  
  35. <tr>  
  36. <td colspan="2">  
  37. You have following contacts in Gmail ID</td>  
  38. </tr>  
  39. <tr>  
  40. <td colspan="2">  
  41. <asp:GridView ID="gridemails" runat="server">  
  42. <EmptyDataTemplate>  
  43. You dont have any contacts yet.  
  44. </EmptyDataTemplate>  
  45. </asp:GridView>  
  46. </td>  
  47. </tr>  
  48. </table>  
  49. </div>  
  50. <div>  
  51. <%--<asp:DropDownList ID="ddlemails" runat="server"></asp:DropDownList>--%>  
  52. </div>  
  53. </form>  
  54. </body>  
  55. </html>  
Code behind
 
Add the following namespaces first and add the .cs code. 
  1. using Google.GData.Contacts;  
  2. using Google.GData.Client;  
  3. using Google.GData.Extensions;  
  4. using Google.Contacts;  
  5. using System.Data;  
  6. using System.Data.SqlClient;  
  7.    
  8. public partial class Default3 : System.Web.UI.Page  
  9. {  
  10. protected void Page_Load(object sender, EventArgs e)  
  11. {  
  12. }  
  13. public DataSet GetGmailContacts(string p_name, string e_id, string psw)  
  14. {  
  15. DataSet ds = new DataSet();  
  16. DataTable dt = new DataTable();  
  17. DataColumn dc = new DataColumn();  
  18. dc.DataType = Type.GetType("System.String");  
  19. dc.ColumnName = "emailid";  
  20. dt.Columns.Add(dc);  
  21. RequestSettings rs = new RequestSettings(p_name, e_id, psw);  
  22. rs.AutoPaging = true;  
  23. ContactsRequest cr = new ContactsRequest(rs);  
  24. Feed<Contact> f = cr.GetContacts();  
  25. foreach (Contact t in f.Entries)  
  26. {  
  27. foreach (EMail email in t.Emails)  
  28. {  
  29. DataRow dr1 = dt.NewRow();  
  30. dr1["emailid"] = email.Address.ToString();  
  31. dt.Rows.Add(dr1);  
  32. }  
  33. }  
  34. ds.Tables.Add(dt);  
  35. return ds;  
  36. }  
  37. public void getcontacts()  
  38. {  
  39. DataSet ds = GetGmailContacts("MyNetwork Web Application!", txtgmailusername.Text, txtpassword.Text);  
  40. gridemails.DataSource = ds;  
  41. gridemails.DataBind();  
  42. }  
  43. protected void Button1_Click(object sender, EventArgs e)  
  44. {  
  45. getcontacts();  
  46. }  
  47. }  
Save all the work and run your Web application.