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.

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
1
using
Google.Contacts;
2
using
Google.GData.Client;
3
using
Google.GData.Contacts;
4
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
01
protected
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
01
Protected
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
18
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
20 comments:
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
Wow really impressive....
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\
@sonu: plz correct ur system date and then try running the code again
@Rajesh :
Have u added the directive mentioned below in ur code behind and add reference to it ?
using Google.GData.Contacts;
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
Very nice, thanks for the information.
This comment has been removed by the author.
dude you write asp articles but u still rely on blogspot wat a shame
Nice article.
Excellent blog
Best code sir/mam...it really work n thanx a lot.
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
dude,
I want to import gmail contacts with profile pics also. Please send me sample code to
vincent.kola@gmail.com
For the C# example:
Feed feedContacts = cRequest.GetContacts();
should be
Feed feedContacts = cRequest.GetContacts();
(contact wasn't capitalized)
hey nice article ,
but i want to retrive single contact is it possible ...?
please help....
@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
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.
@Above: this error is related to your web host, please check permissions as well
I want to Create / Update / Delete Contact in Gmail using VB.NET
Post a Comment