Import Gmail Contact’s in GridView and Send Mail to Selected Email id’s

Prabhakar
Posted by in ASP.NET category on for Intermediate level | Points: 250 | Views : 24248 red flag
Rating: 4.5 out of 5  
 6 vote(s)

In this article I am fetching all Gmail contacts in Gridview . Many time we required our mail id contact list. In article I am trying to do this . All contacts are fetched in Gridview with name, mailed, home no, phone no etc.



 

Firstly add this DLL’s

Google.GData.Contacts.dll

Google.GData.Client.dll

Google.GData.Extensions.dll


In .aspx Page

Firstly add namespaces then write the code.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using Google.GData.Client;

using Google.GData.Extensions;

using Google.GData.Contacts;

using Google.Contacts;

using System.Collections;

using System.Data;

using System.Xml;

using System.Text;

using System.Net;

using System.Net.Mail;

In .apsx page add controls  Textboxs, Button’s , then Gridview. In Import Button Write this Code for Fill Gridview with Gmail Contacts

{

mailid=txtid.Text;

        pwd=txtpassword.Text;

        RequestSettings rs = new RequestSettings("ApplicationName", mailid, pwd);//Application Name,Username,password

        ContactsRequest cr = new ContactsRequest(rs);//Request all contacts

        rs.AutoPaging = true;//Allow autopaging

        Feed<Contact> f = cr.GetContacts();//Get all contacts

        DataTable dt = new DataTable();

        DataRow dr;

        dt.Columns.Add("Name");

        dt.Columns.Add("Home Emails");

        dt.Columns.Add("Work Emails");

        dt.Columns.Add("Other Emails");

        dt.Columns.Add("Home Phone");

        dt.Columns.Add("Work Phone");

        dt.Columns.Add("Other");

        dt.Columns.Add("URL");

        //Get All Contacts

        foreach (Contact ex in f.Entries)

        {

            dr = dt.NewRow();

            Name n = ex.Name;

            dr[0] = n.FullName;

            string homeemail = "";

            string workemail = "";

            string otheremail = "";

            string homephone = "";

            string workphone = "";

            string otherphone = "";

            foreach (EMail email in ex.Emails)

            {

                if (email.Home == true)

                {

                    if (homeemail.Equals(""))

                    {

                        homeemail += email.Address;

                    }

                    else

                    {

                        homeemail += ",";

                        homeemail += email.Address;

                    }

                }

                if (email.Work == true)

                {

                    if (workemail.Equals(""))

                    {

                        workemail += email.Address;

                    }

                    else

                    {

                        workemail += ",";

                        workemail += email.Address;

                    }

                }

                else

                {

                    if (otheremail.Equals(""))

                    {

                        otheremail += email.Address;

                    }

                    else

                    {

                        otheremail += ",";

                        otheremail += email.Address;

                    }

                }

                //dr[1] = homeemail;

                //dr[2] = workemail;

                dr[3] = otheremail;

            }

            //Extract Phone Numbers

            foreach (PhoneNumber ph in ex.Phonenumbers)

            {

                if (ph.Home == true)

                {

                    if (homephone.Equals(""))

                    {

                        homephone += ph.Value;

                    }

                    else

                    {

                        homephone += ",";

                        homephone += ph.Value;

                    }

 

                }

                else if (ph.Work == true)

                {

                    if (workphone.Equals(""))

                    {

                        workphone += ph.Value;

                    }

                    else

                    {

                        workphone += ",";

                        workphone += ph.Value;

                    }

 

                }

                else

                {

                    if (otherphone.Equals(""))

                    {

                        otherphone += ph.Value;

                    }

                    else

                    {

                        otherphone += ",";

                        otherphone += ph.Value;

                    }

                }

 

                dr[4] = homephone;

                dr[5] = workphone;

                dr[6] = otherphone;

            }

            dt.Rows.Add(dr);

        }

 

        GridView1.DataSource = dt;

        GridView1.DataBind();

}

In Send mail Button write this code  

 

GridViewRow row = GridView1.Rows[i];

            bool isChecked = ((CheckBox)row.FindControl("chkSelect")).Checked;

 

            if (isChecked)

            {

                // Column 2 is the name column

 

                str.Append(GridView1.Rows[i].Cells[5].Text);

 

 

                try

                {

 

                    SmtpClient smtpClient = new SmtpClient();

                    smtpClient.Host = "smtp.gmail.com";

                    smtpClient.UseDefaultCredentials = false;

                    smtpClient.EnableSsl = true;

                    smtpClient.Credentials = new NetworkCredential(txtid.Text, txtpassword.Text);

                    smtpClient.Port = 25;

 

                 

                    MailAddress fromAddress = new MailAddress(txtid.Text, txtname.Text);

                    MailMessage message = new MailMessage();

                    message.From = fromAddress;

                    message.To.Add(new MailAddress(GridView1.Rows[i].Cells[5].Text));

                    message.Subject = "TestMail1";

                

                    message.IsBodyHtml = true;

                    string msg = " I am  " + GridView1.Rows[i].Cells[2].Text + " My Email :" + GridView1.Rows[i].Cells[5].Text + "<br/> and <br/>My Enquiry  " + txtfeedback.Text;

                    message.Body = msg;

                    smtpClient.Send(message);

                    lblstatus.Text = "Email successfully sent.";

                }

                catch (Exception ex)

                {

                    lblstatus.Text = "Send Email Failed.<br>" + ex.Message;

                }

            }

        }

Conclusion

After Fill the grid view write the message given below textbox Feedback. I am write “I am Sending . .. “ then after  You select check Box in Gridview Which One You want to Send Mail .

 Then Click Send mail. . Enjoy Code!

Page copy protected against web site content infringement by Copyscape

About the Author

Prabhakar
Full Name: prabhakar parihar
Member Level:
Member Status: Member,MVP
Member Since: 1/12/2011 5:05:40 AM
Country: India
Best Regard's Prabhakar
http://www.dotnetfunda.com
Currently working as a Software Engineer (Developer) [ ASP.NET , C# ]

Login to vote for this post.

Comments or Responses

Posted by: Naimishforu on: 2/9/2011 | Points: 25
Hi Prabhakar,

One more nice article from you.

Thanks for the same.

Can you please also provide downloadable code :) if poss.

Thanks
Posted by: Naimishforu on: 2/9/2011 | Points: 25
Hi,

Also if you can tell from where I can get those DLLs.

Google.GData.Contacts.dll

Google.GData.Client.dll

Google.GData.Extensions.dll

Thanks
Posted by: Karthikanbarasan on: 2/9/2011 | Points: 25
Hi,

Nice one... please provide the links to download the reference dlls
Posted by: Prabhakar on: 2/10/2011 | Points: 25
Hi Naimishforu..

thanks For Responding ... Surely i am attaching Source Code as possible . .
Posted by: Prabhakar on: 2/10/2011 | Points: 25
Hi Karthikanbarasan


thanks For responding . as possible i am attaching Link or Source Code .. .
Posted by: Samarmir on: 2/10/2011 | Points: 25
Very nice and interesting article.
Thanks
Posted by: Bugwee on: 2/11/2011 | Points: 25
Hi Prabhakar,

Nice article... i got difficulties when trying to do this affair but you made it..

Thank You.
Posted by: Prabhakar on: 2/12/2011 | Points: 25
Hi, Samarmir and Bugwee


thanks for responding . .
Posted by: Madhu.b.rokkam on: 2/14/2011 | Points: 25
Nice article.. Keep it going prabhakar
Posted by: Schatak on: 3/4/2011 | Points: 25
Can you please provide downloadable code ?

Thanks in advance
Posted by: Nil1401 on: 12/2/2011 | Points: 25
Thank you very much..
Such a wonderful article for new Developers in contact importer..
Posted by: Sakthi.Singaravel on: 4/13/2012 | Points: 25
Thank u very much...
nice..
Posted by: Kkdubey on: 5/11/2012 | Points: 25
Thanks for the great Article Prabhakar.....

Regards
Krishna
Posted by: Ankit_Parmar555 on: 5/18/2012 | Points: 25
having error in this code.
it gives error for Google.GData.Contacts.dll.
name not fond
Could you please help. ?
Thank you
Posted by: Bhupentiwari on: 7/11/2012 | Points: 25
please provide the full source code including DLL

Login to post response

Comment using Facebook(Author doesn't get notification)