Import Contacts From Gmail by Using ASP.Net and C#

For getting contacts from Gmail, first we need Google data DLLs, that can be downloaded from here.

http://google-gdata.googlecode.com/files/Google%20Data%20API%20Setup(1.4.0.2).msi

Download the installer and install, this installer will install the Google data API.

After that add the reference of these DLLs into your project and include the namespace into your class.

using Google.GData.Contacts;
using Google.GData.Client;
using Google.GData.Extensions;
using Google.Contacts;

After that you need to create RequestSettings and ContactsRequest

So first we create the object of RequestSettings and pass that object to ContactsRequest as in the following:

RequestSettings _requestSettings = new RequestSettings("applicationName", "GmailUserID", "GmailPassword");
ContactsRequest _contactsRequest = new ContactsRequest(_requestSettings);

After that we will call the
GetContacts method as in the following:

Feed<Contact> ContactList = _contactsRequest.GetContacts();

The preceding method will return a list of contacts.

For demo purposes I have created a aspx page that will ask for your Gmail userid and password and when you enter valid credentials, it displays all the friend's Email.

Code for aspx page:

<form id="form1" runat="server">
    <div>
        <table>
            <tr>
                <td colspan="2">
                    <asp:Label ID="LabelErrorMessage" runat="server" ForeColor="Red" Visible="false"></asp:Label>
                </td>
            </tr>
            <tr>
                <td>
                    <strong>Enter userid: </strong>
                </td>
                <td>
                    <asp:TextBox ID="TextBoxUserID" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <strong>Enter Password: </strong>
                </td>
                <td>
                    <asp:TextBox ID="TextBoxPassword" runat="server" TextMode="Password"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                </td>
                <td>
                    <asp:Button ID="ButtonImportContacts" runat="server" Text="Import Contacts" OnClick="ButtonImportContacts_Click" />
                </td>
            </tr>
        </table>
    </div>
    <div>
        <asp:Repeater ID="RepeaterContactsList" runat="server">
            <HeaderTemplate>
                <b>Friends EmailID</b>
            </HeaderTemplate>
            <ItemTemplate>
                <br />
                <%# DataBinder.Eval(Container.DataItem, "EmailID")%>
            </ItemTemplate>
        </asp:Repeater>
    </div>
    </form>

Code for .cs page

using System;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using Google.GData.Contacts;
using Google.GData.Client;
using Google.GData.Extensions;
using Google.Contacts;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public List<GmailContacts> ImportContacts(string applicationName, string userName, string userPassword)
    {
        List<GmailContacts> emailList = new List<GmailContacts>();
        RequestSettings _requestSettings = new RequestSettings(applicationName, userName, userPassword);
        _requestSettings.AutoPaging = true;
        ContactsRequest _contactsRequest = new ContactsRequest(_requestSettings);
        Feed<Contact> ContactList = _contactsRequest.GetContacts();
        try
        {
            LabelErrorMessage.Visible = false;
            foreach (Contact contact in ContactList.Entries)
            {
                foreach (EMail email in contact.Emails)
                {
                    GmailContacts gc = new GmailContacts();
                    gc.EmailID = email.Address;
                    emailList.Add(gc);
                }
            }

        }

        catch (Exception ex)
        {
            LabelErrorMessage.Visible = true;
            LabelErrorMessage.Text = ex.Message;
        }

        return emailList;

    }

    protected void ButtonImportContacts_Click(object sender, EventArgs e)
    {
        List<GmailContacts> ImportList = new List<GmailContacts>();
        ImportList = ImportContacts("ImportContacts", TextBoxUserID.Text, TextBoxPassword.Text);
        RepeaterContactsList.DataSource = ImportList;
        RepeaterContactsList.DataBind();
    }
}

public class GmailContacts
{
    private string _EmailID;
 

    public string EmailID
    {
        get
        {
            return _EmailID;
  
      }

        set
        {
            _EmailID = value;
        }
    }
}

By this way you can easily import Gmail contacts.

Next Recommended Readings