In this blog, learn how to import gmail data (email id from gmail) in ASP.NET. You must provide a gmail account (user id and password) to authenticate. The code uses Google Data API.
Step 1: Download the latest version of Google_Data_API_Setup_2.1.0.0.msi and install to your machine from
https://code.google.com/p/google-gdata/downloads/list. Make sure the find the right location and version of the msi. The location of the download may have moved.
Step 2: Create a Web Application in Visual Studio and design a page. Here is my .aspx page look like.
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="import_gmail.aspx.cs" Inherits="import_gmail" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head id="Head1" runat="server">
  5. <title></title>
  6. <style type="text/css">
  7. .style1
  8. {
  9. width: 100%;
  10. }
  11. </style>
  12. </head>
  13. <body>
  14. <form id="form1" runat="server">
  15. <div style="height: 100px; background-color: #808080">
  16. </div>
  17. <div align="center">
  18. <table class="style1">
  19. <tr>
  20. <td>
  21. </td>
  22. <td>
  23. </td>
  24. <td>
  25. </td>
  26. <td>
  27. </td>
  28. </tr>
  29. <tr>
  30. <td>
  31. </td>
  32. <td>
  33. Enter your Gmail id</td>
  34. <td>
  35. <asp:TextBox ID="txt_email" runat="server" Width="250px"></asp:TextBox>
  36. <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
  37. ControlToValidate="txt_email" ErrorMessage="*"></asp:RequiredFieldValidator>
  38. </td>
  39. <td>
  40. </td>
  41. </tr>
  42. <tr>
  43. <td>
  44. </td>
  45. <td>
  46. Enter your Gmail Password</td>
  47. <td>
  48. <asp:TextBox ID="txtpass" runat="server" TextMode="Password" Width="250px"></asp:TextBox>
  49. <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
  50. ControlToValidate="txtpass" ErrorMessage="*"></asp:RequiredFieldValidator>
  51. </td>
  52. <td>
  53. </td>
  54. </tr>
  55. <tr>
  56. <td>
  57. </td>
  58. <td align="right">
  59. <asp:Button ID="Button1" runat="server" onclick="Button1_Click"
  60. Text="Get List" />
  61. </td>
  62. <td>
  63. <asp:Label ID="lbl_email_count" runat="server" Text="Label"></asp:Label>
  64. </td>
  65. <td>
  66. </td>
  67. </tr>
  68. <tr>
  69. <td>
  70. </td>
  71. <td>
  72. </td>
  73. <td>
  74. <asp:GridView ID="GV_List" runat="server" EnableModelValidation="True"
  75. PageSize="25" AutoGenerateColumns="False">
  76. <Columns>
  77. <asp:BoundField DataField="emailid" HeaderText="Email List" />
  78. </Columns>
  79. </asp:GridView>
  80. </td>
  81. <td>
  82. </td>
  83. </tr>
  84. <tr>
  85. <td>
  86. </td>
  87. <td>
  88. </td>
  89. <td>
  90. </td>
  91. <td>
  92. </td>
  93. </tr>
  94. <tr>
  95. <td>
  96. </td>
  97. <td>
  98. </td>
  99. <td>
  100. </td>
  101. <td>
  102. </td>
  103. </tr>
  104. </table>
  105. </div>
  106. </form>
  107. </body>
  108. </html>
Step 3: Add Referance to four DLLs from C:\Program Files\Google\Google Data API SDK\Redist or the location where you installed Google SDK.
  1. Google.GData.Apps.dll
  2. Google.GData.Client.dll
  3. Google.GData.Contacts.dll
  4. Google.GData.Extensions.dll
Step 4: Here is the code behind .cs file and code.
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using Google.GData.Contacts;
  8. using Google.GData.Client;
  9. using Google.GData.Extensions;
  10. using Google.Contacts;
  11. using System.Data;
  12. using System.Data.SqlClient;
  13. public partial class import_gmail : System.Web.UI.Page
  14. {
  15. protected void Page_Load(object sender, EventArgs e)
  16. {
  17. }
  18. public DataSet GetGmailContacts(string p_name, string e_id, string psw)
  19. {
  20. DataSet ds = new DataSet();
  21. DataTable dt = new DataTable();
  22. DataColumn dc = new DataColumn();
  23. dc.DataType = Type.GetType("System.String");
  24. dc.ColumnName = "emailid";
  25. dt.Columns.Add(dc);
  26. try
  27. {
  28. RequestSettings rs = new RequestSettings(p_name, e_id, psw);
  29. rs.AutoPaging = true;
  30. ContactsRequest cr = new ContactsRequest(rs);
  31. Feed<Contact> f = cr.GetContacts();
  32. foreach (Contact t in f.Entries)
  33. {
  34. foreach (EMail email in t.Emails)
  35. {
  36. DataRow dr1 = dt.NewRow();
  37. dr1["emailid"] = email.Address.ToString();
  38. dt.Rows.Add(dr1);
  39. }
  40. }
  41. ds.Tables.Add(dt);
  42. }
  43. catch (Exception ex)
  44. {
  45. lbl_email_count.Text = "There is a Problem in Id Or Password!!!";
  46. }
  47. return ds;
  48. }
  49. protected void Button1_Click(object sender, EventArgs e)
  50. {
  51. getcontacts();
  52. }
  53. public void getcontacts()
  54. {
  55. DataSet ds = GetGmailContacts("Web Application!", txt_email.Text, txtpass.Text);
  56. if (ds.Tables.Count < 1)
  57. {
  58. lbl_email_count.Text = "No Contacts Found!!!!";
  59. }
  60. else
  61. {
  62. lbl_email_count.Text = "Total Emails from your list :: " + ds.Tables[0].Rows.Count.ToString();
  63. GV_List.DataSource = ds;
  64. GV_List.DataBind();
  65. }
  66. }
  67. }
For more details and code sample, download the attached project, Zip file.