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!