Import Gmail Contacts In Asp.Net

Import Gmail Contacts In Asp.Net 2.0,3.5,4.0 Using Google Data API C# And VB.NET. Several times we need to create web applications which require to import or fetch Gmail Contacts or address book. This example will explain how to fetch or import Gmail contacts in Asp.net web applications.

Import Gmail Contacts In Asp.Net
For Importing Gmail Contacts in asp.net application we need to download Google Data API and install on 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


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

Add two text box and one list box on aspx page and design it to look better.

Add one button to the page for importing the Gmail contacts or address book in Click Event.


HTML Markup Of Page
<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
using Google.Contacts;
using Google.GData.Client;
using Google.GData.Contacts;
using 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
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);
            }
        }

    }

VB.NET Code Behind
Protected Sub btnContacts_Click(sender As Object, e As EventArgs)
 'Provide Login Information
 Dim rsLoginInfo As New RequestSettings("", txtEmail.Text, txtPassword.Text)
 rsLoginInfo.AutoPaging = True

 ' Fetch contacts and dislay them in ListBox
 Dim cRequest As New ContactsRequest(rsLoginInfo)
 Dim feedContacts As Feed(Of Contact) = cRequest.GetContacts()
 For Each gmailAddresses As Contact In feedContacts.Entries
  Console.WriteLine(vbTab + gmailAddresses.Title)
  lstContacts.Items.Add(gmailAddresses.Title)
  For Each emailId As EMail In gmailAddresses.Emails
   Console.WriteLine(vbTab + emailId.Address)
   lstContacts.Items.Add(" " + emailId.Address)
  Next
 Next

End Sub


Now this should show contacts in list box

Hope this helps


Download sample code attached


If you like this post than join us or share

22 comments:

sonu said...

Error in Browser:
The remote certificate is invalid according to the validation procedure.

Error in code:
Execution of request failed: http://www.google.com/m8/feeds/contacts/default/full

Happy New Year Amit Bhai
Plz send me mail my email id sonuraj681@gmail.com


funny stuff said...

Wow really impressive....


Rajesh Chekuri said...

Really searching for this ...

But i got errors

Error 1 The type or namespace name 'contact' could not be found (are you missing a using directive or an assembly reference?)
D:\DotNet-MOSS\DotNet Samples\ASP\ImportGmailContacts\Default.aspx.cs 29 15 D:\...\ImportGmailContacts\

Error 2 Cannot implicitly convert type 'Google.GData.Client.Feed' to 'Google.GData.Client.Feed' D:\DotNet-MOSS\DotNet Samples\ASP\ImportGmailContacts\Default.aspx.cs 29 39 D:\...\ImportGmailContacts\

Error 3 Cannot convert type 'contact' to 'Google.Contacts.Contact' D:\DotNet-MOSS\DotNet Samples\ASP\ImportGmailContacts\Default.aspx.cs 30 9 D:\...\ImportGmailContacts\


amiT jaiN said...

@sonu: plz correct ur system date and then try running the code again


amiT jaiN said...

@Rajesh :

Have u added the directive mentioned below in ur code behind and add reference to it ?

using Google.GData.Contacts;


almeda said...

I recently came across your blog and have been reading along. I thought I would leave my first comment. I don't know what to say except that I have enjoyed reading. Nice blog. I will keep visiting this blog very often.
asp programmer


sewa mobil said...

Very nice, thanks for the information.


Markus Vieghofer said...

This comment has been removed by the author.


idea said...

dude you write asp articles but u still rely on blogspot wat a shame


Siva said...

Nice article.


Anonymous said...

Excellent blog


anni kashyap said...

Best code sir/mam...it really work n thanx a lot.


Muhammad Azeem said...

This is a nice article..
Its very easy to understand ..
And this article is using to learn something about it..
asp.net, c#, javascript
Thanks a lot..!


karandeep said...

Hello
Thanks for sharing this wonderful knowledge .
I want to make similar thing but importing contacts from Yahoo, Hotmail(MSN) and Facebook.
Do you have such ind of code for these ?
If you have then please share.... will be grateful to you

Thanks


vincent said...

dude,
I want to import gmail contacts with profile pics also. Please send me sample code to
vincent.kola@gmail.com


Anonymous said...

nice


Anonymous said...

For the C# example:

Feed feedContacts = cRequest.GetContacts();

should be

Feed feedContacts = cRequest.GetContacts();

(contact wasn't capitalized)


Anonymous said...

hey nice article ,
but i want to retrive single contact is it possible ...?
please help....


amiT jaiN said...

@Above: If you want display any perticular contact then you can match this contact in foreach loop and if it mathces then can add to listbox or display


Anonymous said...

this project is working only in localhost, but if we publish this website to server .. it gives error :

The remote server returned an error: (403) Forbidden.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Net.WebException: The remote server returned an error: (403) Forbidden.


amiT jaiN said...

@Above: this error is related to your web host, please check permissions as well


Kathan shah said...

I want to Create / Update / Delete Contact in Gmail using VB.NET


Find More Articles