Advertisements
Home > Csharp, DotNet > Import contacts from Gmail using ASP.Net/C# Application

Import contacts from Gmail using ASP.Net/C# Application


How to import contacts from GMAIL into ASP.Net/C# application

Some of the application especially social network application needs to have the feature for import contacts from our gmail account. By using this feature we can easily import all contacts from our gmail account and can send bulk mail to all or groups in our gmail account

 Is it possible import contacts from GMAIL account using ASP.Net/C# Application?

Of course, we can access and import our gmail contacts into our ASP.Net/C# application using gmail API. GMAIL itself providing some APIs to access gmail accounts from an external applications like ASP.Net or C#. We can call this API from our application by passing our gmail credentials, then will access our account from the application. Here we are demonstrating how we can use GMAIL API to get contacts in GMAIL account using ASP.Net/C# application.

 Very simple Steps to import GMAIL contacts from ASP.Net/C# application

Step-1:  Download Google data API setup.

 Here is the proper link for downloading particular API.

http://google-gdata.googlecode.com/files/Google%20Data%20API%20Setup%281.4.0.2%29.msi

 In this link GMAIL providing so many APIs for communicating with their different project.

http://code.google.com/p/google-gdata/

 Google.GData.Apps.dll, Google.GData.Client.dll, Google.GData.Contacts.dll, Google.GData.Extensions.dll are the major dll that’s should use for our application.

 Step-2:  Create a very simple ASP.Net application to import contacts from GMAIL. For a simple application, aspx page looks like this

 <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>IMport Gmail Contacts</title>
</head>
<body>
<form id="frmGmailContacts" runat="server">
<div>
<table>
<tr>
<td>
UserName</td>
<td>
<asp:TextBox ID="txtUsername" 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="btnImport" runat="server" Text="Import"
onclick="btnImport_Click" />
</td>
</tr>
</table>
</div>
<div>
<asp:GridView ID="gdvContacts" runat="server"></asp:GridView>
</div>
</form>
</body>
</html>
 

Step 3 : In code behind we have to call API for getting contacts on button click event, and assign result set to gridview.

 

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("Import GMAIL Contacts",
txtUsername.Text, txtPassword.Text);
gdvContacts.DataSource = ds;
gdvContacts.DataBind();
}
Advertisements
  1. katharbatsha
    November 23, 2011 at 10:54 am

    Is it possible import contacts from GMAIL account using ASP.Net/C# Application in windows application?

  2. November 30, 2011 at 4:18 am

    Sure we can implemented the same in windows application too. The thing is that we should have internet connectio to call API

  3. December 20, 2011 at 12:26 pm

    I am not able to get the profile pic of any contact. I have the code given in developer’s guide.

    Please help!!!!

  4. December 21, 2011 at 4:21 am

    Hi Isha Jain, Can u please post the code that you using now. Or send me the code to infotuvian@gmail.com

  5. December 21, 2011 at 4:48 am

    Firstly i used this code:

    foreach (Contact t in f.Entries)
    {
    Stream photo = cr.GetPhoto(t);

    if (photo != null)
    {
    dr1[“Profile Pic”] = photo;

    }
    It gives content not modified error

    Then i used this code:

    Stream photo = cr.Service.Query(t.PhotoUri);
    dr1[“Profile Pic”] = System.Drawing.Image.FromStream(photo);

    It is also not working.

    I need it urgently. Can u please help?

  6. December 21, 2011 at 4:50 am

    Please post your email id

  7. December 21, 2011 at 5:07 am
  8. December 21, 2011 at 7:30 am

    Hi Isha,
    Please include Google.GData.Photos.dll and use following codes.
    foreach (Contact t in f.Entries)
    {
    string photoUrl = t.PhotoUri ;
    }

    Here you can get the path of the image.

    Please try and let me know the result
    thanks

  9. December 21, 2011 at 9:48 am

    i got the path of the image. But this is the string variable. I want to show the image in gridview.

    And also when i copy the path and open it in browser, it do not show any image and says that Error 401

  10. December 21, 2011 at 12:38 pm

    Stream s = cr.Service.Query(t.PhotoUri);

    This is working fine. That means photo accessed and converted to stream.
    In windows application we can create a picturebox control and bind this photo to the picturebox control using following code.

    pictureBox1.Image = Image.FromStream(s);

  11. December 22, 2011 at 6:00 am

    Ya i tried this also. But it says that FromStream is not supported by System.Eb.Controls.Image.

  12. December 22, 2011 at 6:05 am

    It says that ‘System.Web.UI.WebControls.Image’ does not contain a definition for ‘FromStream’

  13. December 22, 2011 at 6:46 am

    And moreover it says that GDataRequestException was unhandled by user code. It crashes and gives errror:

    The remote server returned an error: (404) Not Found.

  14. December 22, 2011 at 8:57 am

    First of all, for using Google APIs you have to register your application in the Google. Then only we can send authorized request to Google.
    Please refer link
    http://iwanttobrowsetheweb.appspot.com/code.google.com/p/google-api-dotnet-client/wiki/OAuth2

  15. February 6, 2012 at 8:48 am

    ContactsRequest and contacts is missing..

  16. mona
    October 12, 2012 at 11:54 am

    hello,

    i am implementing code in my site but i am facing following error:

    invalid credential.

    Locally works fine but in online server i am getting the above error. I guess this error is occurs because i am not keep the application name in RequestSettings rs = new RequestSettings(App_Name, Uname, UPassword); but i dont know how to get application name. so please help me.

  17. October 12, 2012 at 3:54 pm

    Hi Mona,

    You have to put your application name in the place of ‘App_Name’ variable.
    In order to get authorization for your application name, you have to register your application name in the Google API.

    Please refer following link more details

    http://iwanttobrowsetheweb.appspot.com/code.google.com/p/google-api-dotnet-client/wiki/OAuth2

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: