In this page, you will find, how to import contacts from Gmail with the help of Google data API which Google has provided us to import Gmail contacts, and also different .NET Address Book API's to import contacts from Gmail, Yahoo, MSN. Please find the sample code in ASP.Net ( C# language )





Please find below sample ASP.Net code to import contacts from Gmail.

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

Contacts.aspx
<div>
    <table>
        <tr>
            <td>
                <asp:Label ID="lblEmail" runat="server" Text="Email:">asp:Label>
            td>
            <td>
                <asp:TextBox ID="txtEmail" runat="server">asp:TextBox>
            td>
        tr>
        <tr>
             <td>
                <asp:Label ID="lblPassword" runat="server" Text="Password:">asp:Label>
            td>
            <td>
                <asp:TextBox ID="txtPwd" runat="server" TextMode="Password">asp:TextBox>
            td>
        tr>
        <tr>
            <td colspan="2">
                <asp:Button ID="btnImportContacts" runat="server" Text="Import Contacts"
                    onclick="btnImportContacts_Click" />
            td>        
        tr>
        <tr>
            <td colspan="2">
                <asp:GridView ID="grdContacts" runat="server" AutoGenerateColumns="false">
                    <Columns>
                        <asp:BoundField DataField="Title" HeaderText="Title" />
                        <asp:BoundField DataField="EmailID" HeaderText="EmailID" />
                    Columns>
                asp:GridView>
            td>                
        tr>
    table>
div>

Contacts.aspx.cs

public class Contacts
{
    private string _EmailID;
    private string _Title;
 
    public string EmailID
    {
       get
       {
            return _EmailID;
        }
 
        set
        {
            _EmailID = value;
        }
    }
 
    public string Title
    {
        get
        {
            return _Title;
        }
 
        set
        {
            _Title = value;
        }
    }
}



protected void btnImportContacts_Click(object sender, EventArgs e)
{
      RequestSettings reqSettings = new RequestSettings("HimasagarContacts", txtEmail.Text, txtPwd.Text);
      reqSettings.AutoPaging = true;
 
      ContactsRequest contReq = new ContactsRequest(reqSettings);
      Feed<Contact> Contacts = contReq.GetContacts();
 
      List<GmailContacts> objContacts = new List<GmailContacts>();
       
      foreach (Contact objContact in Contacts.Entries)
      {
            GmailContacts objGmailContacts = new GmailContacts();
            objGmailContacts.Title = objContact.Title.ToString();
 
            foreach (EMail emailId in objContact.Emails)
            {
                 objGmailContacts.EmailID = emailId.Address.ToString() + ",";
            }
            if (objGmailContacts.EmailID != null)
            {
                 objGmailContacts.EmailID = objGmailContacts.EmailID.Substring(0, objGmailContacts.EmailID.Length - 1);
            }
            objContacts.Add(objGmailContacts);
     }
 
     grdContacts.DataSource = objContacts;
     grdContacts.DataBind();
}