Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How to import Gmail contacts in ASP.NET

Author: Mike Green
Download Source Code : 1443_WebApplication1.zip

Following article will describe how to import Gmail contacts in your ASP.NET Application.

Many users need their email contacts in an application or in a website to invite or send emails. Most of the social network sites provide this facility for users because they believe that a good application is one that minimizes the user efforts when users use their application. These sites are actually importing contacts from Gmail, Yahoo, MSN etc and using it for their purpose. Many other applications may also need to import contacts from Gmail, Yahoo, MSN etc for their purpose. In recent times, I was creating an application and I was in need to import contacts only from Gmail in that application. I have written code for this purpose and I have decided to share it with you so that you can also take advantage of it.

 
Importing Gmail contacts in ASP.NET is not a big deal. You just need to use Google data API which Google has provided us to import Gmail contacts. So download Google data API and add following dlls to your application bin folder. I have written my code in C# but in this article, I am providing you code both in C# and VB.NET.
 
Google.GData.Apps.dll
Google.GData.Client.dll
Google.GData.Contacts.dll
Google.GData.Extensions.dl
 
Now add following namespaces in your code behind file
 
C#          
 
using Google.Contacts;
using Google.GData.Client;
using Google.GData.Contacts;
using Google.GData.Extensions;
 
VB.NET
 
Imports Google.Contacts
Imports Google.GData.Client
Imports Google.GData.Contacts
Imports Google.GData.Extensions
 
Add below code in aspx page
 
<div>
    <table>
        <tr>
            <td>
                <asp:Label ID="lblEmail" runat="server" Text="Email:">asp:Label>
            td>
            <td>
                <asp:TextBox ID="txtEmail" runat="server">asp:TextBox>
            td>
        tr>
        <tr>
             <td>
                <asp:Label ID="lblPassword" runat="server" Text="Password:">asp:Label>
            td>
            <td>
                <asp:TextBox ID="txtPwd" runat="server" TextMode="Password">asp:TextBox>
            td>
        tr>
        <tr>
            <td colspan="2">
                <asp:Button ID="btnImport" runat="server" Text="Import Contacts"
                    onclick="btnImport_Click" />
            td>        
        tr>
        <tr>
            <td colspan="2">
                <asp:GridView ID="grdContacts" runat="server" AutoGenerateColumns="false">
                    <Columns>
                        <asp:BoundField DataField="Title" HeaderText="Title" />
                        <asp:BoundField DataField="EmailID" HeaderText="EmailID" />
                    Columns>
                asp:GridView>
            td>                
        tr>
    table>
div>

 

Add following class in your web application
 
C#
 
public class GmailContacts
{
    private string _EmailID;
    private string _Title;
 
    public string EmailID
    {
       get
       {
            return _EmailID;
        }
 
        set
        {
            _EmailID = value;
        }
    }
 
    public string Title
    {
        get
        {
            return _Title;
        }
 
        set
        {
            _Title = value;
        }
    }
}
 
VB.NET
 
Public Class GmailContacts
    Private _EmailID As String
    Private _Title As String
 
    Public Property EmailID() As String
        Get
            Return _EmailID
        End Get
 
        Set(ByVal value As String)
            _EmailID = value
        End Set
    End Property
 
    Public Property Title() As String
        Get
            Return _Title
        End Get
 
        Set(ByVal value As String)
            _Title = value
        End Set
    End Property
End Class

 

Now add below code in button click event
 
C#
 
protected void btnImport_Click(object sender, EventArgs e)
{
      RequestSettings reqSettings = new RequestSettings("My Application", txtEmail.Text, txtPwd.Text);
      reqSettings.AutoPaging = true;
 
      ContactsRequest contReq = new ContactsRequest(reqSettings);
      Feed<Contact> Contacts = contReq.GetContacts();
 
      List<GmailContacts> objContacts = new List<GmailContacts>();
       
      foreach (Contact objContact in Contacts.Entries)
      {
            GmailContacts objGmailContacts = new GmailContacts();
            objGmailContacts.Title = objContact.Title.ToString();
 
            foreach (EMail emailId in objContact.Emails)
            {
                 objGmailContacts.EmailID = emailId.Address.ToString() + ",";
            }
            if (objGmailContacts.EmailID != null)
            {
                 objGmailContacts.EmailID = objGmailContacts.EmailID.Substring(0, objGmailContacts.EmailID.Length - 1);
            }
            objContacts.Add(objGmailContacts);
     }
 
     grdContacts.DataSource = objContacts;
     grdContacts.DataBind();
}
 
VB.NET

Private Sub btnImport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnImport.Click
    Dim reqSettings As New RequestSettings("My Application", txtEmail.Text, txtPwd.Text)
    reqSettings.AutoPaging = True
 
    Dim contReq As New ContactsRequest(reqSettings)
    Dim Contacts As Feed(Of Contact) = contReq.GetContacts()
 
    Dim objContacts As New List(Of GmailContacts)()
    For Each objContact As Contact In Contacts.Entries
        Dim objGmailContacts As New GmailContacts()
        objGmailContacts.Title = objContact.Title.ToString()
 
        For Each emailId As EMail In objContact.Emails
                objGmailContacts.EmailID = emailId.Address.ToString() + ","
        Next
        If objGmailContacts.EmailID IsNot Nothing Then
            objGmailContacts.EmailID = objGmailContacts.EmailID.Substring(0, objGmailContacts.EmailID.Length - 1)
        End If
        objContacts.Add(objGmailContacts)
    Next
 
    grdContacts.DataSource = objContacts
    grdContacts.DataBind()
End Sub

 
First you need to provide the login information of the person you want to import contacts. Then you can call the GetContact() method of the ContactsRequest object. At the end you can loop through the contact list to use it for your purpose. Here I am only displaying title and email address of a contact.

 

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to count characters in a text box using JavaScript in ASP.NET

Disclaimer - Privacy
© 2002-2017 DevASP.net