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
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


17 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.
nice...
maybe you want to check out my blog
http://thatsthaway.blogspot.com
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.
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..!
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
nice
For the C# example:
Feed feedContacts = cRequest.GetContacts();
should be
Feed feedContacts = cRequest.GetContacts();
(contact wasn't capitalized)
Post a Comment