What you want to see on DotNetFunda.com ?
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 4733 |  Welcome, Guest!   Register  Login
Home > Articles > ASP.NET > Import Gmail Contact’s in GridView and Send Mail to Selected Email id’s

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

6 vote(s)
Rating: 4.5 out of 5
Article posted by Prabhakar on 2/9/2011 | Views: 12230 | Category: ASP.NET | Level: Intermediate | Points: 250 red flag


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!

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

Experience:3 year(s)
Home page:http://www.dotnetfunda.com
Member since:Wednesday, January 12, 2011
Level:Starter
Status: [Member] [MVP]
Biography:Currently working as a Software Engineer (Developer)
[ ASP.NET , C# ]
 Responses
Posted by: Naimishforu | Posted on: 09 Feb 2011 10:07:34 AM | 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 | Posted on: 09 Feb 2011 10:25:53 AM | 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 | Posted on: 09 Feb 2011 01:23:43 PM | Points: 25

Hi,

Nice one... please provide the links to download the reference dlls

Posted by: Prabhakar | Posted on: 10 Feb 2011 12:55:36 AM | Points: 25

Hi Naimishforu..

thanks For Responding ... Surely i am attaching Source Code as possible . .

Posted by: Prabhakar | Posted on: 10 Feb 2011 12:56:45 AM | Points: 25

Hi Karthikanbarasan


thanks For responding . as possible i am attaching Link or Source Code .. .

Posted by: Samarmir | Posted on: 10 Feb 2011 08:12:03 AM | Points: 25

Very nice and interesting article.
Thanks

Posted by: Bugwee | Posted on: 11 Feb 2011 09:43:42 PM | Points: 25

Hi Prabhakar,

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

Thank You.

Posted by: Prabhakar | Posted on: 12 Feb 2011 01:50:03 AM | Points: 25

Hi, Samarmir and Bugwee


thanks for responding . .

Posted by: Madhu.b.rokkam | Posted on: 14 Feb 2011 02:40:03 AM | Points: 25

Nice article.. Keep it going prabhakar

Posted by: Schatak | Posted on: 04 Mar 2011 05:23:40 AM | Points: 25

Can you please provide downloadable code ?

Thanks in advance

Posted by: Nil1401 | Posted on: 02 Dec 2011 05:15:01 AM | Points: 25

Thank you very much..
Such a wonderful article for new Developers in contact importer..

Posted by: Sakthi.Singaravel | Posted on: 13 Apr 2012 10:15:51 PM | Points: 25

Thank u very much...
nice..

Posted by: Kkdubey | Posted on: 11 May 2012 09:35:11 AM | Points: 25

Thanks for the great Article Prabhakar.....

Regards
Krishna

Posted by: Ankit_Parmar555 | Posted on: 18 May 2012 12:39:57 AM | 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 | Posted on: 11 Jul 2012 07:22:15 AM | Points: 25

please provide the full source code including DLL

>> Write Response - Respond to this post and get points
Related Posts

In this article we will understand MVP, execute a sample project with MVP, implement the same using windows UI and then finally we will discuss about the differences between MVP and MVC.

In this Article you can learn how to get a Selected Row Data from a DataList and display the data in textBoxes.

In case we have to give ability to the user to select the columns that should appear in the GridView, we can follow this approach.

In scenarios where we want to validate a textbox for the specific range of values, and ensure that the correct Date to be entered, we can follow this approach.

I am going to explain about the reading data from xml and filtering particular element and binding into Grid view using ASP.NET ,C#.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/25/2013 2:41:36 PM