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