Wednesday, March 16, 2011

Import gmail address using asp.net c#

Import email addresses like in any social networking site.Below example we have used to fetch the gmail address.
For this we require Google Data API and install the same to get the required dlls.
Add these dlls as reference in your project.
1. Google.GData.Client
2. Google.GData.Contacts
3. Google.GData.Extensions
Use .net framework 3.5 or 4.0

Code

Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Google.Contacts;
using Google.GData.Client;
using Google.GData.Contacts;
using Google.GData.Extensions;

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

        }

        #region FETCH EMAIL IDS
        private List FetchEmailIds(string emailid, string password)
        {
            List emaillist = new List();
            try
            {

                RequestSettings Logindetails = new RequestSettings("", emailid, password);
                Logindetails.AutoPaging = true;
                ContactsRequest getRequest = new ContactsRequest(Logindetails);
                Feed listContacts = getRequest.GetContacts();
                foreach (Contact mailAddress in listContacts.Entries)
                {
                    // Response.Write(mailAddress.Title + "
");
                    foreach (EMail emails in mailAddress.Emails)
                    {
                        EmailLists emailaddress = new EmailLists();
                        emailaddress.Title = mailAddress.Title;
                        emailaddress.EmailAddress = emails.Address;
                        //Response.Write(emails.Address + "
");
                        emaillist.Add(emailaddress);
                    }

                }

            }
            catch (Exception)
            {

                Label1.Text = "Sorry your email id  or password does not match.";
            }
            return emaillist;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            GridView1.DataSource = FetchEmailIds(txtEmailId.Text, txtPassword.Text);
            GridView1.DataBind();

        }


        #endregion
    }

}

EmailLists.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace GmailContacts
{
    public class EmailLists
    {
        public string Title { get; set; }
        public string EmailAddress { get; set; }

    }
}

Download Code

7 comments:

  1. This is amaging and easy to integrate.

    Sir Do you have any such thing Yahoo and AOL grabber

    Thanks

    Prashant

    ReplyDelete
    Replies
    1. nice one but iam using 3.5 framework,im adding dll in to bin folder but there is no effect on namespace side."there is no name or object reference "google" like this error will be displaying what im doing now reply

      Delete
  2. nice one but iam using 3.5 framework,im adding dll in to bin folder but there is no effect on namespace side."there is no name or object reference "google" like this error will be displaying what im doing now reply

    ReplyDelete
  3. You can download all the dlls from google.com

    ReplyDelete
  4. yes i have downloaded all the dlls from google.com like
    using Google.GData.Contacts;
    using Google.GData.Client;
    using Google.GData.Extensions;
    using Google.Contacts;
    but raising an error on "Google" same error in every page

    ReplyDelete
  5. I am using the same code but it works with some id But
    It is not working with some another gmail id ?

    ReplyDelete
  6. Execution of authentication request returned unexpected result: 404
    how to overcome this error.

    ReplyDelete