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#.

Design:

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>How to Send Gridview in Email Body in Asp.Net Using C#</title>

<style type="text/css">

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

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

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

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

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

<br /><br />

All rights reserved by <a href="http://www.hightechnology.in">www.Hightechnology.in</a> | Hosting partner <a href="http://www.grootstech.com" target="_blank">Grootstech</a>
</div>
</form>
</body>
</html>

Code:

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)
        {
            binddata();
        }
    }
    public void binddata()
    {
        SqlConnection con = new SqlConnection(strcon);
        try
        {
            con.Open();
        }
        catch { }
        string str = "select top(5) * from users";
        SqlCommand cmd = new SqlCommand(str, con);
        SqlDataReader reader;
        reader = cmd.ExecuteReader();
        gv.DataSource = reader;
        gv.DataBind();
        con.Close();
    }
    protected void sendgvdata_Click(object sender, EventArgs e)
    {
        sendmail();
    }
    public void sendmail()
    {
        MailMessage mail = new MailMessage();
        mail.From = new MailAddress("demo@gmail.com");
        mail.To.Add("info@hightechnology.in");
       
        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("smtp.gmail.com", 587);
        smtp.Credentials = new System.Net.NetworkCredential("demo@gmail.com", "demo@123");
        smtp.EnableSsl = true;
        smtp.Send(mail);
    }

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