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.
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="import_gmail.aspx.cs" Inherits="import_gmail" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head id="Head1" runat="server">
- <title></title>
- <style type="text/css">
- .style1
- {
- width: 100%;
- }
- </style>
- </head>
- <body>
- <form id="form1" runat="server">
- <div style="height: 100px; background-color: #808080">
- </div>
- <div align="center">
- <table class="style1">
- <tr>
- <td>
- </td>
- <td>
- </td>
- <td>
- </td>
- <td>
- </td>
- </tr>
- <tr>
- <td>
- </td>
- <td>
- Enter your Gmail id</td>
- <td>
- <asp:TextBox ID="txt_email" runat="server" Width="250px"></asp:TextBox>
- <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
- ControlToValidate="txt_email" ErrorMessage="*"></asp:RequiredFieldValidator>
- </td>
- <td>
- </td>
- </tr>
- <tr>
- <td>
- </td>
- <td>
- Enter your Gmail Password</td>
- <td>
- <asp:TextBox ID="txtpass" runat="server" TextMode="Password" Width="250px"></asp:TextBox>
- <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
- ControlToValidate="txtpass" ErrorMessage="*"></asp:RequiredFieldValidator>
- </td>
- <td>
- </td>
- </tr>
- <tr>
- <td>
- </td>
- <td align="right">
- <asp:Button ID="Button1" runat="server" onclick="Button1_Click"
- Text="Get List" />
- </td>
- <td>
- <asp:Label ID="lbl_email_count" runat="server" Text="Label"></asp:Label>
- </td>
- <td>
- </td>
- </tr>
- <tr>
- <td>
- </td>
- <td>
- </td>
- <td>
- <asp:GridView ID="GV_List" runat="server" EnableModelValidation="True"
- PageSize="25" AutoGenerateColumns="False">
- <Columns>
- <asp:BoundField DataField="emailid" HeaderText="Email List" />
- </Columns>
- </asp:GridView>
- </td>
- <td>
- </td>
- </tr>
- <tr>
- <td>
- </td>
- <td>
- </td>
- <td>
- </td>
- <td>
- </td>
- </tr>
- <tr>
- <td>
- </td>
- <td>
- </td>
- <td>
- </td>
- <td>
- </td>
- </tr>
- </table>
- </div>
- </form>
- </body>
- </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.
- Google.GData.Apps.dll
- Google.GData.Client.dll
- Google.GData.Contacts.dll
- Google.GData.Extensions.dll
Step 4: Here is the code behind .cs file and code.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using Google.GData.Contacts;
- using Google.GData.Client;
- using Google.GData.Extensions;
- using Google.Contacts;
- using System.Data;
- using System.Data.SqlClient;
- public partial class import_gmail : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- public DataSet GetGmailContacts(string p_name, string e_id, string psw)
- {
- DataSet ds = new DataSet();
- DataTable dt = new DataTable();
- DataColumn dc = new DataColumn();
- dc.DataType = Type.GetType("System.String");
- dc.ColumnName = "emailid";
- dt.Columns.Add(dc);
- try
- {
- RequestSettings rs = new RequestSettings(p_name, e_id, psw);
- rs.AutoPaging = true;
- ContactsRequest cr = new ContactsRequest(rs);
- Feed<Contact> f = cr.GetContacts();
- foreach (Contact t in f.Entries)
- {
- foreach (EMail email in t.Emails)
- {
- DataRow dr1 = dt.NewRow();
- dr1["emailid"] = email.Address.ToString();
- dt.Rows.Add(dr1);
- }
- }
- ds.Tables.Add(dt);
- }
- catch (Exception ex)
- {
- lbl_email_count.Text = "There is a Problem in Id Or Password!!!";
- }
- return ds;
- }
- protected void Button1_Click(object sender, EventArgs e)
- {
- getcontacts();
- }
- public void getcontacts()
- {
- DataSet ds = GetGmailContacts("Web Application!", txt_email.Text, txtpass.Text);
- if (ds.Tables.Count < 1)
- {
- lbl_email_count.Text = "No Contacts Found!!!!";
- }
- else
- {
- lbl_email_count.Text = "Total Emails from your list :: " + ds.Tables[0].Rows.Count.ToString();
- GV_List.DataSource = ds;
- GV_List.DataBind();
- }
- }
- }
For more details and code sample, download the attached project, Zip file.

Pankajkumar PatelPosted Sep 9, 2019, 11:14 PM
Nice article
kalu singh raoPosted Nov 26, 2015, 5:43 AM
good one
aditya immadiPosted May 22, 2013, 10:18 AM
Error 2 Make sure that the class defined in this code file matches the 'inherits' attribute, and that it extends the correct base class (e.g. Page or UserControl). ...i got dis error can u fix it thanks