Pages

Monday, May 30, 2011

Import Gmail Contacts in Asp.Net

First of all download Google Data API and install to get following dlls
1. Google.GData.Client
2. Google.GData.Contacts
3. Google.GData.Extensions
Put these 3 dlls in BIN folder of application also add reference to your project for them.

using Google.Contacts;
using Google.GData.Client;
using Google.GData.Contacts;
using Google.GData.Extensions;
<b>Email: </b>

<asp:TextBox ID="txtEmail" runat="server"> </asp:TextBox>
<b>Password : </b>
<asp:TextBox ID="txtPassword" runat="server" TabIndex="1" TextMode="Password">
</asp:TextBox>
<asp:Button ID="btnContacts" runat="server"  onclick="btnContacts_Click"
TabIndex="2" Text="Import Contacts"  Width="125px" />
<asp:ListBox ID="lstContacts" runat="server" Height="176px" Width="229px">
 </asp:ListBox>

protected void btnContacts_Click(object sender, EventArgs e)
    {
        //Provide Login Information
        RequestSettings rsLoginInfo = new RequestSettings("", txtEmail.Text, txtPassword.Text);
        rsLoginInfo.AutoPaging = true;

        // Fetch contacts and dislay them in ListBox
        ContactsRequest cRequest = new ContactsRequest(rsLoginInfo);
        Feed <contact> feedContacts = cRequest.GetContacts();
        foreach (Contact gmailAddresses in feedContacts.Entries)
        {
            Console.WriteLine("\t" + gmailAddresses.Title);
            lstContacts.Items.Add(gmailAddresses.Title);
            foreach (EMail emailId in gmailAddresses.Emails)
            {
                Console.WriteLine("\t" + emailId.Address);
                lstContacts.Items.Add(" " + emailId.Address);
            }
        }

    }

1 comment: