How to Import Contacts From Gmail in Asp.Net C#

In this Tutorial I am Going to Share With You How to Import Google Contacts or How to Fetch Contacts from Gmail. or And also explain How to Retrieve Contacts from Gmail.

Import Contacts From Gmail in Asp.Net C#

Generally, While we working on Asp.net Project in many case it is requre to import contacts from gmail and display in tabuler formate and i got same requrement. In this tutorial we will create one social networking website and in which we will provide facility to user can import His/Her contacts from Gmail and that contacts is Bind with Gridview.

To Import Contacts From Google mail (Gmail) Google provide one kind of Functionality. Google provide Google Contacts data API. which is used to import users contacts from gmail.

So, Lets Create one webpage for Demonstration purpose:

You need to just follow this steps which is listed below to import contacts from gmail.

Step 1: Download Google Data API from this given link Click Hear to Download
http://code.google.com/p/google-gdata/

After Download API that Setup will installs set of Google Data dll’s file such as Google.GData.Apps.dll, Google.GData.Client.dll, Google.GData.Contacts.dll, Google.GData.Extensions.dll and many other into client's Machine, you should collect that dll’s for your program.

Step 2: Now, create one webpage with simple design. (.aspx)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ImportGmailcontacts.aspx.cs" Inherits="ImportGmailcontacts" %>
 
<!DOCTYPE html>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .auto-style1 {
            height50px;
            text-aligncenter;
            color#FFFFFF;
            background-color#FF9900;
        }
        .auto-style2 {
            color#FFFFFF;
            height34px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
            <table border="0">
                
                <tr>
                    <td colspan="2" class="auto-style1"><strong>Import Contacts Form Gmail
                    </strong>
                    </td>
                </tr>
                <tr>
                    <td>Gmail User Name</td>
                    <td>
                        <asp:TextBox ID="txt_Username" placeholder="Username" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>Gmail Password</td>
                    <td>
                        <asp:TextBox ID="txt_Password" runat="server" placeholder="Password" TextMode="Password"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <center>
                        <asp:Button ID="FetchContactsfromgmail" runat="server" Text="Fetch Contacts from gmail" BackColor="#009900" BorderStyle="None" ForeColor="White" Height="34px" Width="238px" OnClick="FetchContactsfromgmail_Click" />
                            </center>
                    </td>
                </tr>
                <tr>
                    <td colspan="2" class="auto-style2" style="text-aligncenterbackground-color#000000">
                        Copyright © 2016 Asppoint All Right Reserved.</td>
                </tr>
            </table>
        </div>
        <div>
            <asp:GridView ID="gvgmaildata" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">
                <AlternatingRowStyle BackColor="White" />
                <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
                <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
                <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
                <SortedAscendingCellStyle BackColor="#FDF5AC" />
                <SortedAscendingHeaderStyle BackColor="#4D0000" />
                <SortedDescendingCellStyle BackColor="#FCF6C0" />
                <SortedDescendingHeaderStyle BackColor="#820000" />
            </asp:GridView>
        </div>
    </form>
</body>
</html>
Step 3: Now, All Those Downloaded google dlls as a refrence to your website.
For Add Reference You should Go to in Solution Explorer >> Right Click on Your Project Name >> Click on Add Reference >> Click on Brows Button >> Select ddls from Installed Location >> Click OK.

Step4: Write C# Code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Web.Security;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Google.GData.Contacts;
using Google.GData.Client;
using Google.GData.Extensions;
using Google.Contacts;
using System.Data;
 
public partial class ImportGmailcontacts : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
    protected void FetchContactsfromgmail_Click(object sender, EventArgs e)
    {
        DataSet ds = GetGmailData("My Web Application!", txt_Username.Text, txt_Password.Text);
        gvgmaildata.DataSource = ds;
        gvgmaildata.DataBind();
    }
    public static DataSet GetGmailData(string App_Name, string GmailUserName, string GmailPassword)
    {
        DataSet dsd = new DataSet();
        DataTable dtd = new DataTable();
        DataColumn Clmn = new DataColumn();
        Clmn.DataType = Type.GetType("System.String");
        Clmn.ColumnName = "EmailID";
        dtd.Columns.Add(Clmn);
        RequestSettings rs = new RequestSettings(App_Name, GmailUserName, GmailPassword);
        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 rows = dtd.NewRow();
                rows["EmailID"] = email.Address.ToString();
                dtd.Rows.Add(rows);
            }
        }
        dsd.Tables.Add(dtd);
        return dsd;
    }
}
Previous
Next Post »

If you have any kind of question about any post, Feel free to ask.You can simply drop a comment below post. Your feedback and suggestions will be highly appreciated. ConversionConversion EmoticonEmoticon