Home > ASP.Net > Send Selected Data from Gridview into Email in ASP.Net

Send Selected Data from Gridview into Email in ASP.Net


Try this example to Send Selected Data from Gridview into Email

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<form id="form1" runat="server">
<asp:GridView ID="gv_Category" runat="server" AutoGenerateColumns="False" CellPadding="4"
DataKeyNames="CategoryID" DataSourceID="sds_Category" EnableModelValidation="True"
<AlternatingRowStyle BackColor="White" />
<asp:CheckBox ID="chkSelect" runat="server" />
<asp:TemplateField HeaderText="Category ID" InsertVisible="False" SortExpression="CategoryID">
<asp:Label ID="lblCategoryID" runat="server" Text='<%# Bind("CategoryID") %>'></asp:Label>
<asp:TemplateField HeaderText="Category Name" SortExpression="CategoryName">
<asp:Label ID="lblCategoryName" runat="server" Text='<%# Bind("CategoryName") %>'></asp:Label>
<asp:TemplateField HeaderText="Description" SortExpression="Description">
<asp:Label ID="lblDescription" runat="server" Text='<%# Bind("Description") %>'></asp:Label>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<asp:SqlDataSource ID="sds_Category" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT [CategoryID], [CategoryName], [Description] FROM [Categories]">
<asp:Button ID="btn_SendEmail" runat="server" onclick="btn_SendEmail_Click"
Text="Send Email" />

In Code behind :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Net;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.IO;
using System.Text;
using System.Net.Mime;

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

protected void btn_SendEmail_Click(object sender, EventArgs e)
string sAttchent = string.Empty;
string attachmentPath = string.Empty;

MailMessage mm = new MailMessage();
mm.From = new MailAddress("From@gmail.com");

string sContent = string.Empty;
sContent = "
<table border='1'>
<td>Category Name</td>
<td>Category Description</td>
foreach (GridViewRow row in gv_Category.Rows)
CheckBox chkSelect = (CheckBox)row.FindControl("chkSelect");
if (chkSelect.Checked)
int rowIndex = row.RowIndex;
string CategoryID = Convert.ToString(gv_Category.DataKeys[rowIndex].Values["CategoryID"]);
Label lblCategoryName = (Label)row.FindControl("lblCategoryName");
Label lblDescription = (Label)row.FindControl("lblDescription");
sContent += "
<td>" + lblCategoryName.Text + "</td>
<td>" + lblDescription.Text + "</td>
string sCategoryName = string.Empty;
string sCategoryImage = string.Empty;
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "SELECT * FROM Categories WHERE CategoryID = " + CategoryID;
cmd.Connection = new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
if (dt.Rows.Count > 0)
for (int i = 0; i < dt.Rows.Count; i++)
MemoryStream memoryStream = new MemoryStream();
byte[] contentAsBytes = (byte[])dt.Rows[i]["Picture"];
memoryStream.Write(contentAsBytes, 78, contentAsBytes.Length - 78);
// Set the position to the beginning of the stream.
memoryStream.Seek(0, SeekOrigin.Begin);
// Create attachment
ContentType contentType = new ContentType();
contentType.MediaType = "image/png";
contentType.Name = dt.Rows[i]["CategoryName"].ToString();
Attachment attachment = new Attachment(memoryStream, contentType);
// Add the attachment

mm.Subject = "Category Details ";
mm.IsBodyHtml = true;
mm.Body = "this content is in the body
" + sContent + "</table>
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
NetworkCredential credential = new NetworkCredential();
credential.UserName = "sender@gmail.com";
credential.Password = "xxxxxx";
smtp.UseDefaultCredentials = true;
smtp.Credentials = credential;
smtp.Port = 587;
smtp.EnableSsl = true;

In Web.Config add Connection String

<add name="NorthwindConnectionString" connectionString="Data Source=YASSER-PC;Initial Catalog=Northwind;Integrated Security=True" providerName="System.Data.SqlClient"/>

Hope this helps

Good Luck.

Categories: ASP.Net
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: