Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

how to import Contacts from GMAIL using ASP.NET and C#

Apr 2, 2010
Introduction:

In this article I will explain how to import gmail contacts of a user by using GContacts Data API provided by Google.

Description:

We have developed one social network site at that time we think that how to popularize the our social network site I have checked some of social network sites at that time we got idea to implement import contacts concept is best to intimate all of our friends at a time I have searched so many websites to implement this concepts but no result finally I have implemented import contacts from gmail 


I used ASP.NET and C# for developing this application.
We need to follow below steps to get gmail contacts 

Step-1:  Download Google data API setup from the specified URL 

Or you can directly download with the following

That Set Up will installs set of Google Data dll’s (Google.GData.Apps.dll, Google.GData.Client.dll, Google.GData.Contacts.dll, Google.GData.Extensions.dll) into client installed Machine, you should collect that dll’s for your program.

Step-2:  Design our aspx page (Default.aspx) by using with the following UI as simple scenario.


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>IMport Gmail Contacts</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
UserName</td>
<td>
<asp:TextBox ID="txtgmailusername" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Password</td>
<td>
<asp:TextBox ID="txtpassword" runat="server" TextMode="Password"></asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</td>
</tr>
</table>
</div>
<div>
<asp:GridView ID="gvmails" runat="server"></asp:GridView>
</div>
</form>
</body>
</html>
Step-3:  Add those downloaded google dll’s as reference to your website in visual studio->Solution Explorer ->Right Click-> Click on Add Reference….->Browse ->Get dll’s from Installed Location->Press OK.

Step-4:
A) Add namespace these namespace to your code behind

using Google.GData.Contacts;
using Google.GData.Client;
using Google.GData.Extensions;
using Google.Contacts;
B) After that write following code in code behind


public static DataSet GetGmailContacts(string App_Name, string Uname, string UPassword)
{
DataSet ds = new DataSet();
DataTable dt = new DataTable();
DataColumn C2 = new DataColumn();
C2.DataType = Type.GetType("System.String");
C2.ColumnName = "EmailID";
dt.Columns.Add(C2);
RequestSettings rs = new RequestSettings(App_Name, Uname, UPassword);
rs.AutoPaging = true;
ContactsRequest cr = new ContactsRequest(rs);
Feed<Contact> f = cr.GetContacts();
foreach (Contact t in f.Entries)
{
foreach (EMail email in t.Emails)
{
DataRow dr1 = dt.NewRow();
dr1["EmailID"] = email.Address.ToString();
dt.Rows.Add(dr1);
}
}
ds.Tables.Add(dt);
return ds;
}
protected void Button1_Click(object sender, EventArgs e)
{
DataSet ds = GetGmailContacts("MyNetwork Web Application!", txtgmailusername.Text, txtpassword.Text);
gvmails.DataSource = ds;
gvmails.DataBind();
}
Demo

Note:Keep Remember You Need Internet Connection @ the Time of Executing This Application.
I hope it helps you 

Download sample code attached

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

64 comments :

Unknown said...

But i am unable to call references for the dll(s) in class file.

However i have successfully did this in VS 4.0 but the same i can't do in VS 3.5.

Please suggest !

Suresh Dasari said...

Hi Mantu
Please check the attached sample that is working in VS 3.5

SohelElite said...

Can u also post code for import contacts from Yahoo & Hotmail.

Unknown said...

superb........

Please post code for import contacts from Yahoo & Facebook

Srinath said...

its really superb suresh.thank u

Chamara Iresh said...

i need AOL Contact import API Code

Anonymous said...

Can u please post the code for retrieving gmail contact profile pic.I have tried the codes in developer's guide but not able to get the pic.

Anonymous said...

It works like a charm. Keep up the good work! Thanks Suresh!

Anonymous said...

How to Change PassWord Character from .... to ****.

kishor said...

nice........

Please post code for import contacts from Yahoo & Facebook

baghali said...

hi,
that code was realy nice but there is some problem with that,
no one supposed that secure to enter her/his gmail ID.people used to opening a new page in Gmail domain to enter their IDs there.

ragu said...

Thnaks Buddy its Work Fine for me for gmail..thank you..agan

i need it for for hotmail and yahoo can you can you it it for me...

Anonymous said...

Very good post suresh. Can you tell me how you have code this demo part.

Anonymous said...

I have learnt many things from this site and this type of content is not available on other site.

Anonymous said...

Your articles are really Mind blowing...They help me great every times.I have also followed your Facebook Login. Your articles really fullfill my 98% requirements.
Thanks for posting your valuable resources..!!

Anonymous said...

please help me. how can i get application name?

Rajkumar Palnati said...

Hi Bro
I did this fine.But i want to add s.no to this gridview as a column SNo. k it will be appeared but in the jump format like only even no's or odd no's
i'm using
==============================
DataColumn C1 = new DataColumn("SNo", typeof(int));
C1.ColumnName = "SNo";
dt.Columns.Add(C1);
C1.AutoIncrement = true;
C1.AutoIncrementSeed = 1;
C1.AutoIncrementStep = 1;

======================
pls send me how to add sno to the above grdvw.

Thanks in advance bro

Puneet said...

Great job............very good artical

suman said...

very gr8 job suresh..i really like it

suman said...

very gr8 job suresh..i really like it

san said...

hi Suresh,

I got error that is Google.GData.Client.InvalidCredentialsException: Invalid credentials.
But i gave correct username password.
In local its working good.
What happen.?

Anupam Gupta said...

hiii.. this example is very helpful for me for fecthing my gmail contact, but i also get my gmail profile information like my name, email,gender etc.. so how can i fecth this info in my application.. help me

Kesavakumar said...

hi Suresh, this example is very helpful for me for fetching my g-mail contact, I want to get online contact list from g-mail can you send me your suggestions and code to my mail-id kesavakumarv@gmail.com. Thanks in advance.

Anonymous said...

hi suresh...
can u post to import all latest email from inbox using gmail account.

Anonymous said...

Dear sir
I want to create an application like gtalk where automatical reply option is available for user

Anonymous said...

foreach (Contact t in f.Entries)
in this case i have error what shoiuld i do? i need ur answers guys.........

Rajnish Azad said...

Hi Sir,

thank you very much. for this article.

sagar's DEN said...

hello,I am Sagar J
my all contacts are importing when i run my project on local host,but when i publish the web application on server it gives me error 'Invalid Credentials'
my code is as below--
//Provide Login Information
RequestSettings rsLoginInfo = new RequestSettings("http://demo.vishwainfosoft.com/TalentAcquisition/TA_Home.aspx", txtGEmail.Text, txtPassword.Text);
rsLoginInfo.AutoPaging = true;

// Fetch contacts and dislay them in ListBox
ContactsRequest cRequest = new ContactsRequest(rsLoginInfo);
Feed feedContacts = cRequest.GetContacts();
try
{
foreach (Contact gmailAddresses in feedContacts.Entries)
{
Console.WriteLine("\t" + gmailAddresses.Title);
//lstContacts.Items.Add(gmailAddresses.Title);

//chklContacts.DataBind();
foreach (EMail emailId in gmailAddresses.Emails)
{
Console.WriteLine("\t" + emailId.Address);
//lstContacts.Items.Add(" " + emailId.Address);

chklContacts.Items.Add(emailId.Address);
ViewState["TCount"] = chklContacts.Items.Count;
foreach (ListItem item in chklContacts.Items)
item.Selected = true;
lblTotalContacts.Text = "Total" + " " + ViewState["TCount"].ToString() + " " + "records found...";
}

}
}
catch(Exception ex)
{
lblTotalContacts.Text = ex.Message;
}
chklContacts.DataBind();

Unknown said...

Great Article.. I just tried it out and worked like a charm..
The only confusion I have is that, if contacts can be imported from gmail using the method you have shared, what is the benefit of using the Google Contact API via OAuth 2.0 and access tokens?
Would really appreciate if you could throw some light on this.
Thanks a lot.

Suman Biswas said...

Hi Suresh,
I have tried your code and it can extract gmail contacts successfully when it is running from local machine (India location) but when trying to upload server and trying to extract then gmail suspecting its suspicious attempt and blocking login.
Do you have any information how to solve this problem?

Thanks for writing so many codes for others.

Suman

Unknown said...

Hi sir,I have tried ur code it is working fine but i want to extract the contacts of yahoo also.how it can be done?
Do you have any information regarding this problem?

Unknown said...

Hi sir,I have tried ur code it is working fine but i want to extract the contacts of yahoo also.how it can be done?
Do you have any information regarding this problem?

Anonymous said...

Hi,
Can u provide the API for Yahoo and Hotmail also.

<a href="http://hotmail.com" target="_blank">Hotmail</a>

Unknown said...

Hi Sir

This very good and awesome article

Can you write program like this for Invite Facebook and twitter and LinkedIn contracts?? and publish us please??

Unknown said...

hiiiii... suresh
I want to show all mails into my own site, so any code for this probelm

Anonymous said...

helo

I want to store all gmail in dtabase.
any help?

Thanks In advance

boopalan said...

How to import Facebook and MSN and yahoo contacts in c# using asp.net please sent me details in this id:emails4boopalan@gmail.com ,,i am awaiting bosss...

about us said...

Very insightful idea. thanks.

8008094502 said...

I Have Security to My EmailId I can't Get Contacts now.It Shows Invalid Credentials

Anonymous said...

Great Articule, how I can get username and picture, not only de email? thanks

Master Moin said...

Good Job Suresh

work fine with there is not exists two step verification but give error

Google.GData.Client.InvalidCredentialsException: Invalid credentials

when user has enable two step verification service

Please provide me solution what i have to do when two step verification come in picture and how do i come to know user has active two step verification or not

Anonymous said...

PLEASE ANSWER TO ALL QUESTIONS ABOVE SURESH

subhash choudhary said...

thats good work dear but when im going to implement my project vs2010 that giving one error in foreach loop

showing error that : Email is a namespace but like used type . can u help me.

RSS said...

hi its great tutorial..nicew its works fine..but how can i check credentials values ..before

Unknown said...
This comment has been removed by the author.
Anonymous said...

hai sir..i want to invite facebook friends for an event in to my website so plz help me

Anonymous said...

how to handle invalid credential exception using gdata while importing contacts in asp.net C#

Anonymous said...

hi sir,its not working its saying invalid credentials please can you tell me the issue

Mrweb said...

the code is not working anymore
google blocks it

Unknown said...

nice.........its Work
How Can we get all Contact information from Gmail account using Email ID
Plz Help me Sir..

Unknown said...
This comment has been removed by the author.
Unknown said...

this is not working getting error message the error is google convert http to https

SAMIR R. BHOGAYTA said...

hi..
How can I download my contacts photo's with the help of this application. Can you please reply as early as possible.

Unknown said...

Hello,
I have read all comment. There are many comment like "Invalid credentials".
But there is not answer for this issue.
Please call me if you have any solve
I am getting same error. I am sure i have enter correct username and password.

Unknown said...

Hello,
Here attached code sample not working in VS 3.5 and 4.0
anyone getting solution so please suggest here...

Anonymous said...

Execution of authentication request returned unexpected result

Daftar Yahoo Mail said...

Thank you for the information, I think this article is very useful for all who read it.
.

Rashmi ranjan ransingh said...

Execution of authentication request returned unexpected result: 404

HUSEN said...

Execution of authentication request returned unexpected result: 404

how can i solve this

Tanmoy said...

how to get my email id list if my gmail is already logged in?

Arvind said...

how to import Contacts from GMAIL using MVC4,Linq

Vikash Kumar said...

"Execution of authentication request returned unexpected result: 404".I get the error in this line, foreach (Contact t in f.Entries)

Unknown said...

Execution of authentication request returned unexpected result: 404

Anonymous said...

I am trying to run above code but its giving me below error please suggest

An exception of type 'Google.GData.Client.GDataRequestException' occurred in Google.GData.Client.dll but was not handled in user code

Additional information: Execution of authentication request returned unexpected result: 404

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.