How to Send GridView in Email Body in Asp.Net Using C#

How to Send GridView in Email Body in Asp.Net Using C#

In this post we will learn How to Send GridView in Email Body in Asp.Net Using C#. In earlier post we have learnt how to Send Email with Attachment in Asp.Net c#.


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

<!DOCTYPE html>

<html xmlns="">
<head runat="server">
<title>How to Send Gridview in Email Body in Asp.Net Using C#</title>

<style type="text/css">

 width: 980px;
 margin: 0px auto;
 text-align: center;
 padding-top: 50px;
 font-size: 20px;
<form id="form1" runat="server">
<h2>How to Send Gridview in Email Body in Asp.Net Using C#</h2>
<asp:GridView ID="gv" runat="server" Width="100%" AutoGenerateColumns="false">

<asp:TemplateField HeaderText="UID">
<ItemTemplate><asp:Label ID="uidn" runat="server" Text='<%#Eval("uid") %>'></asp:Label></ItemTemplate>

<asp:TemplateField HeaderText="UserName">
<ItemTemplate><asp:Label ID="uname" runat="server" Text='<%#Eval("username") %>'></asp:Label></ItemTemplate>

<asp:TemplateField HeaderText="User Email">
<ItemTemplate><asp:Label ID="uemail" runat="server" Text='<%#Eval("uemail") %>'></asp:Label></ItemTemplate>

<br /><br />
<asp:Button ID="sendgvdata" runat="server" Text="Send GridView Data" OnClick="sendgvdata_Click" />

<br /><br />

All rights reserved by <a href=""></a> | Hosting partner <a href="" target="_blank">Grootstech</a>


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

public partial class _Default : System.Web.UI.Page
    String strcon=ConfigurationManager.ConnectionStrings["connnn"].ConnectionString;
    protected void Page_Load(object sender, EventArgs e)
        if (!Page.IsPostBack)
    public void binddata()
        SqlConnection con = new SqlConnection(strcon);
        catch { }
        string str = "select top(5) * from users";
        SqlCommand cmd = new SqlCommand(str, con);
        SqlDataReader reader;
        reader = cmd.ExecuteReader();
        gv.DataSource = reader;
    protected void sendgvdata_Click(object sender, EventArgs e)
    public void sendmail()
        MailMessage mail = new MailMessage();
        mail.From = new MailAddress("");
        mail.Subject = "GridView Data";
        mail.IsBodyHtml = true;
        mail.Body += "Please check below data Sent by Email <br/><br/>";
        mail.Body += getgvdata(gv);
        SmtpClient smtp = new SmtpClient("", 587);
        smtp.Credentials = new System.Net.NetworkCredential("", "demo@123");
        smtp.EnableSsl = true;

    public string getgvdata(GridView gv)
        StringBuilder strBuilder = new StringBuilder();
        StringWriter strWriter = new StringWriter(strBuilder);
        HtmlTextWriter htw = new HtmlTextWriter(strWriter);
        return strBuilder.ToString();
    public override void VerifyRenderingInServerForm(Control control)
        /* Verifies that the control is rendered */