Click here to Skip to main content
11,708,721 members (71,711 online)
Click here to Skip to main content

Tagged as

ASP.NET: Import Gmail Contacts

, 25 Feb 2013 CPOL 15.2K 19
Rate this:
Please Sign up or sign in to vote.
ASP.NET: Import Gmail Contacts

Introduction

Most of the time our clients need to use their email contacts from their application, they believe in the good application which reduces their effort to go other websites, they know everything can not be possible to implement but not impossible, so here requirement comes to implement a section to import contacts from Gmail and send email to the selected email id from the asp.net application.

A good application is one who can complete your all or maximum work from one browser window or from one application.

I tried here to describe "Import Gmail contacts in ASP.NET". Hope I'll explain it here in a better way so that everyone facing this issue can done this like the way I done.

Requirement

We all know while the implementation of any third party tool in ASP.net we need concerned dlls to add references in the application.

To import Gmail contacts in ASP.NET application we need to download "Google API setup" from the below link

Download Google API setup

This link I'll download following dll’s in to your PC :

  1. Google.GData.Apps.dll
  2. Google.GData.Client.dll
  3. Google.GData.Contacts.dll
  4. Google.GData.Extensions.dll

When you got these dll’s on your PC we need to add these to our application.

Add references in to your application by right click -> add reference ->Browse dll’s -> OK

Source Code

Add below source code to create a design like I created :

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Import gmail contacts in to asp.net | Neha Sharma</title>
    <style type="text/css">
        .auto-style1 {
            height: 26px;
        }
    </style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table align="center" style="height: 104px; width: 301px; background-color: #F0F0F0">
<tr>
<td>
    Gmail ID</td>
<td>
<asp:TextBox ID="txtgmailusername" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style1">
Password</td>
<td class="auto-style1">
<asp:TextBox ID="txtpassword" runat="server" TextMode="Password"></asp:TextBox>
</td>
</tr>
<tr>
<td>
    <asp:Button ID="Button1" runat="server" Text="Get" OnClick="Button1_Click" />
    </td>
<td>
     </td>
</tr>
<tr>
<td colspan="2">
    You have below contacts in Gmail ID</td>
</tr>
<tr>
<td colspan="2">
<asp:GridView ID="gridemails" runat="server">
    <EmptyDataTemplate>
        You dont have any contacts yet.
    </EmptyDataTemplate>
    </asp:GridView>
</td>
</tr>
</table>
</div>
<div>
    <%--<asp:DropDownList ID="ddlemails" runat="server"></asp:DropDownList>--%>
</div>
</form>
</body>
</html>

Code behind :

Add the following namespaces first:

using Google.GData.Contacts;
using Google.GData.Client;
using Google.GData.Extensions;
using Google.Contacts;
using System.Data;
using System.Data.SqlClient;

Now,

public partial class Default3 : System.Web.UI.Page
{
   
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public  DataSet GetGmailContacts(string p_name, string e_id, string psw)
    {
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();
        DataColumn dc = new DataColumn();
        dc.DataType = Type.GetType("System.String");
        dc.ColumnName = "emailid";
        dt.Columns.Add(dc);
        RequestSettings rs = new RequestSettings(p_name, e_id, psw);
        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;
       
            }
   
    public void getcontacts()
    {
        DataSet ds = GetGmailContacts("MyNetwork Web Application!", txtgmailusername.Text, txtpassword.Text);
        gridemails.DataSource = ds;
        gridemails.DataBind();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        getcontacts();
    }
}

Save the whole work and run the page.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

nehas1jan
Software Developer (Junior) TIV Labs
India India
No Biography provided

You may also be interested in...

Comments and Discussions

 
QuestionExecution of authentication request returned unexpected result: 404 Pin
NJ Bhanushali14-Jul-15 19:33
memberNJ Bhanushali14-Jul-15 19:33 
QuestionInvalid credentials error in retrive gmail contacts Pin
kunalnayi11-Mar-15 19:39
memberkunalnayi11-Mar-15 19:39 
QuestionGoogle has block my request. Pin
DevMec29-Sep-14 23:13
memberDevMec29-Sep-14 23:13 
QuestionHow to import mail from gmail with notification Pin
Member 1001725415-Jul-14 20:26
memberMember 1001725415-Jul-14 20:26 
QuestionHow to solve exception? Pin
Mich_906-Apr-14 11:41
memberMich_906-Apr-14 11:41 
AnswerRe: How to solve exception? Pin
michelrl16-May-14 23:49
membermichelrl16-May-14 23:49 
QuestionComments Pin
abdulwahab12324-Feb-14 11:08
memberabdulwahab12324-Feb-14 11:08 
SuggestionIt better to use OAuth to import contacts Pin
Imran Ali18-Sep-13 20:42
memberImran Ali18-Sep-13 20:42 
Questiontype or namespace name 'Contacts' does not exist Pin
neetesh1232-Sep-13 1:42
memberneetesh1232-Sep-13 1:42 
AnswerRe: type or namespace name 'Contacts' does not exist Pin
nehas1jan2-Sep-13 2:29
membernehas1jan2-Sep-13 2:29 
QuestionSuspicious activity Detected error by gmail Pin
Ashok Kumar28-Jul-13 19:06
memberAshok Kumar28-Jul-13 19:06 
AnswerRe: Suspicious activity Detected error by gmail Pin
nehas1jan30-Jul-13 2:06
membernehas1jan30-Jul-13 2:06 
AnswerRe: Suspicious activity Detected error by gmail Pin
हर्ष गुप्ताा13-Nov-14 23:19
memberहर्ष गुप्ताा13-Nov-14 23:19 
AnswerRe: Suspicious activity Detected error by gmail Pin
हर्ष गुप्ताा17-Nov-14 20:22
memberहर्ष गुप्ताा17-Nov-14 20:22 
QuestionAbout Import Gmail Pin
mr.vishal.0527-Jun-13 19:52
groupmr.vishal.0527-Jun-13 19:52 
GeneralMy vote of 5 Pin
vitesh99991-Mar-13 0:34
membervitesh99991-Mar-13 0:34 
QuestionGood tip Pin
ssampath27-Feb-13 17:55
memberssampath27-Feb-13 17:55 
Questionex.Message = "Execution of authentication request returned unexpected result: 407" Pin
Radhakrishnan D26-Feb-13 22:44
memberRadhakrishnan D26-Feb-13 22:44 
Generalgood article Pin
dogancanbakir26-Feb-13 5:31
memberdogancanbakir26-Feb-13 5:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150819.1 | Last Updated 25 Feb 2013
Article Copyright 2013 by nehas1jan
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid