In this article I will explain with an example, how to export GridView to HTML, embed it in email body and then sending it in email message in ASP.Net using C# and VB.Net.
HTML Markup
The following HTML Markup consists of:
GridView – For displaying data.
Button – For sending exported GridView in email.
The Button has been assigned with an OnClick event handler.
<asp:GridView ID="gvCustomers" runat="server" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
RowStyle-BackColor="#A1DCF2" AlternatingRowStyle-BackColor="White" AlternatingRowStyle-ForeColor="#000"
AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" ItemStyle-Width="80" />
<asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
<asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="150" />
</Columns>
</asp:GridView>
<br />
<asp:Button ID="btnSendEmail" runat="server" Text="Send email" OnClick="SendEmail" />
Namespaces
You will need to import the following namespaces
C#
using System.IO;
using System.Data;
using System.Net;
using System.Net.Mail;
using System.Net.Configuration;
using System.Configuration;
VB.Net
Imports System.IO
Imports System.Data
Imports System.Net
Imports System.Net.Mail
Imports System.Net.Configuration
Imports System.Configuration
Binding the GridView
Inside the Page_Load event handler, the GridView is populated with the records from dynamic DataTable.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("Country") });
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(4, "Robert Schidner", "Russia");
gvCustomers.DataSource = dt;
gvCustomers.DataBind();
}
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id"), New DataColumn("Name"), New DataColumn("Country")})
dt.Rows.Add(1, "John Hammond", "United States")
dt.Rows.Add(2, "Mudassar Khan", "India")
dt.Rows.Add(3, "Suzanne Mathews", "France")
dt.Rows.Add(4, "Robert Schidner", "Russia")
gvCustomers.DataSource = dt
gvCustomers.DataBind()
End If
End Sub
Exporting GridView to HTML and sending email with GridView HTML embedded in email body
When SendEmail button is clicked, the email setting is read from SmtpSection section of Web.Config file.
Then, the StringWriter and HtmlTextWriter class objects are created and the GridView is exported to an HTML string using StringReader class.
Finally, the HTML string is embedded as part of email body and the email is sent with the exported GridView embedded in email body.
C#
protected void SendEmail(object sender, EventArgs e)
{
// Read SMTP section from Web.Config.
SmtpSection smtpSection = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter hw = new HtmlTextWriter(sw))
{
gvCustomers.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
using (MailMessage mm = new MailMessage("sender@gmail.com", "receiver@gmail.com"))
{
mm.Subject = "GridView Email";
mm.Body = "GridView:<hr />" + sw.ToString();
mm.IsBodyHtml = true;
using (SmtpClient smtp = new SmtpClient())
{
smtp.Host = smtpSection.Network.Host;
smtp.EnableSsl = smtpSection.Network.EnableSsl;
NetworkCredential networkCred = new NetworkCredential();
networkCred.UserName = smtpSection.Network.UserName;
networkCred.Password = smtpSection.Network.Password;
smtp.UseDefaultCredentials = smtpSection.Network.DefaultCredentials;
smtp.Credentials = networkCred;
smtp.Port = 587;
smtp.Send(mm);
}
}
}
}
}
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
VB.Net
Protected Sub SendEmail(ByVal sender As Object, ByVal e As EventArgs)
' Read SMTP section from Web.Config.
Dim smtpSection As SmtpSection = CType(ConfigurationManager.GetSection("system.net/mailSettings/smtp"), SmtpSection)
Using sw As StringWriter = New StringWriter()
Using hw As HtmlTextWriter = New HtmlTextWriter(sw)
gvCustomers.RenderControl(hw)
Dim sr As StringReader = New StringReader(sw.ToString())
Using mm As MailMessage = New MailMessage("sender@gmail.com", "receiver@gmail.com")
mm.Subject = "GridView Email"
mm.Body = "GridView:<hr />" & sw.ToString()
mm.IsBodyHtml = True
Using smtp As SmtpClient = New SmtpClient()
smtp.Host = smtpSection.Network.Host
smtp.EnableSsl = smtpSection.Network.EnableSsl
Dim networkCred As NetworkCredential = New NetworkCredential()
networkCred.UserName = smtpSection.Network.UserName
networkCred.Password = smtpSection.Network.Password
smtp.UseDefaultCredentials = smtpSection.Network.DefaultCredentials
smtp.Credentials = networkCred
smtp.Port = 587
smtp.Send(mm)
End Using
End Using
End Using
End Using
End Sub
Public Overrides Sub VerifyRenderingInServerForm(control As Control)
' Verifies that the control is rendered
End Sub
Screenshots
The Form
ASP.Net GridView embedded in email body in Gmail Mailbox
Downloads