Monday, June 3, 2013

How To- Import Gmail Contacts in ASP.Net

In this post, I am explaining how can you import the Gmail contacts in your ASP.Net application. For importing Gmail contacts we need some dlls which you can get after download Google Data API and install on your system.

In my previous posts, I explained Export Gridview to PDF in ASP.Net with Image, Send mail in ASP.Net, Convert DataTable into List, Constructor Chainning in C#, Convert a Generic List to a Datatable, Get Property Names using Reflection in C#Hard drive information using C#Create Directory/Folder using C#Check Internet Connection using C#SQL Server Database BackUp using C# and some other articles related to C#ASP.Net jQuery, Java Script and SQL Server.

Create a new web application and add the reference of following dlls in your application.

Google.GData.Client
Google.GData.Contacts
Google.GData.Extensions

Here I am showing the fetched contacts on Gridview, So I have added a Gridviewand two TextBoxes with one button on my aspx page as shown below.
ASPX Page

<form id="form1" runat="server">
    <div>
        <table>
            <tr>
                <td>
                    <asp:Label ID="lblEmail" runat="server" Text="E-mail :"></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="Emails" HeaderText="Email IDs" />
                    </Columns>
                    </asp:GridView>
                </td>
                
            </tr>
        </table>
    </div>
    </form>
On code behind of your page add the following directives-
using Google.Contacts;
using Google.GData.Client;
using Google.GData.Contacts;
using Google.GData.Extensions;
Now write the following code on Click Event of Button-
C# Code

protected void btnImport_Click(object sender, EventArgs e)
{
    //Provide Login Information
    RequestSettings req = new RequestSettings("", txtEmail.Text, txtPwd.Text);
    req.AutoPaging = true;
            
    // Fetch contacts
    ContactsRequest cRequest = new ContactsRequest(req);
    Feed<Contact> fContacts = cRequest.GetContacts();
    //Lisrt of Contacts
    List<ContacsClass> objContacts = new List<ContacsClass>();
    foreach (Contact gmailAddresses in fContacts.Entries)
    {
        ContacsClass obj = new ContacsClass();
        obj.Title = gmailAddresses.Title.ToString();
        foreach (EMail emailId in gmailAddresses.Emails)
        {
            obj.Emails = emailId.Address.ToString() + ",";                    
        }
        if(obj.Emails!=null)
            obj.Emails = obj.Emails.Substring(0, obj.Emails.Length - 1);
        objContacts.Add(obj);
    }
    //Display Fetched Contact Details on GridView
    grdContacts.DataSource = objContacts;
    grdContacts.DataBind();
}

I hope this article will be helpful for you. I would like to have any feedback from you. Your valuable feedback, question, or comments about this article are always welcome.

No comments:

Post a Comment

^ Scroll to Top