Wednesday, February 15, 2012

Import Gmail Contacts In Asp.Net

Several times we need to develop web applications which require to import or fetch Gmail Contacts or address book.

In this example i'm explaining how to fetch or import Gmail contacts in Asp.net web applications using C# and VB.NET.

For getting started we need to download Google Data API and install on the system to get the desired dlls.

Create a new website and visual studio and put these 3 dlls in BIN folder of application from the location google data API has been installed on ur system.

1. Google.GData.Client
2. Google.GData.Contacts
3. Google.GData.Extensions


Now add references to these dlls in ur application by right clicking on solution explorer and select add reference.


Now add two text box and one list box on your aspx page and design it to look better.
Add one button to the page for importing the Gmail contacts or address book

HTML markup of my page look like this
<form id="form1" runat="server">
    <div>
    
        <b>Email Address : </b>
        <br />
        <asp:TextBox ID="txtEmail" runat="server">
        </asp:TextBox>
        <br />
        <br />
        <b>Password : </b>
        <br />
        <asp:TextBox ID="txtPassword" runat="server" 
                     TabIndex="1" TextMode="Password">
        </asp:TextBox>
        <br />
        <br />
        <asp:Button ID="btnContacts" runat="server"  
                    onclick="btnContacts_Click" 
                    TabIndex="2" Text="Import Contacts" 
                    Width="125px" />
        <br />
        <br />
        <br />
        <b>Contacts:<br />
        </b>
        <asp:ListBox ID="lstContacts" runat="server" 
                     Height="176px" 
                     Width="229px">
        </asp:ListBox>
        <br />
        <br />
    
    </div>
    </form>

Go to code behind of aspx page and add directives mentioned below
1using Google.Contacts;
2using Google.GData.Client;
3using Google.GData.Contacts;
4using Google.GData.Extensions;

Now in design view of page double click on button to generate Click event and write below mentioned code in click event of button to fetch or import gmail contacts in list box

C# Code Behind
01protected void btnContacts_Click(object sender, EventArgs e)
02    {
03        //Provide Login Information
04        RequestSettings rsLoginInfo = new RequestSettings("", txtEmail.Text, txtPassword.Text);
05        rsLoginInfo.AutoPaging = true;
06 
07        // Fetch contacts and dislay them in ListBox
08        ContactsRequest cRequest = new ContactsRequest(rsLoginInfo);
09        Feed <contact> feedContacts = cRequest.GetContacts();
10        foreach (Contact gmailAddresses in feedContacts.Entries)
11        {
12            Console.WriteLine("\t" + gmailAddresses.Title);
13            lstContacts.Items.Add(gmailAddresses.Title);
14            foreach (EMail emailId in gmailAddresses.Emails)
15            {
16                Console.WriteLine("\t" + emailId.Address);
17                lstContacts.Items.Add(" " + emailId.Address);
18            }
19        }
20 
21    }

VB.NET Code Behind
01Protected Sub btnContacts_Click(sender As Object, e As EventArgs)
02 'Provide Login Information
03 Dim rsLoginInfo As New RequestSettings("", txtEmail.Text, txtPassword.Text)
04 rsLoginInfo.AutoPaging = True
05 
06 ' Fetch contacts and dislay them in ListBox
07 Dim cRequest As New ContactsRequest(rsLoginInfo)
08 Dim feedContacts As Feed(Of Contact) = cRequest.GetContacts()
09 For Each gmailAddresses As Contact In feedContacts.Entries
10  Console.WriteLine(vbTab + gmailAddresses.Title)
11  lstContacts.Items.Add(gmailAddresses.Title)
12  For Each emailId As EMail In gmailAddresses.Emails
13   Console.WriteLine(vbTab + emailId.Address)
14   lstContacts.Items.Add(" " + emailId.Address)
15  Next
16 Next
17 
18End Sub

1 comment:

  1. Hey what a brilliant post I have come across and believe me I have been searching out for this similar kind of post for past a week and hardly came across this. Thank you very much and will look for more postings from you. Share google contacts

    ReplyDelete