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.
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.