In this article I will explain with an example, how to send
GridView in email in
ASP.Net using C# VB.Net.
HTML Markup
The following HTML Markup consists of:
GridView – For displaying data.
Button – For exporting
GridView and sending in email.
The Button has been assigned with an OnClick event handler.
<asp:GridView ID="gvCustomers" HeaderStyle-BackColor="#3AC0F2"
RowStyle-BackColor="#A1DCF2" AlternatingRowStyle-BackColor="White" AlternatingRowStyle-ForeColor="#000"
runat="server" 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.Configuration;
using System.Net.Configuration;
VB.Net
Imports System.IO
Imports System.Data
Imports System.Net
Imports System.Net.Mail
Imports System.Configuration
Imports System.Net.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
Mail Server Settings in Web.Config file
The following Mail Server settings need to be saved in the
Web.Config file.
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="sender@gmail.com">
<network
host="smtp.gmail.com"
port="587"
enableSsl="true"
userName="sender@gmail.com"
password="GMAILor2STEP-PASSWORD"
defaultCredentials="true"/>
</smtp>
</mailSettings>
</system.net>
MailMessage class properties
Following are the required properties of the MailMessage class.
From – Sender’s email address.
To – Recipient(s) Email Address.
CC – Carbon Copies. (If any)
BCC – Blind Carbon Copies. (If any)
Subject – Subject of the Email.
Body – Body of the Email.
IsBodyHtml – Specify whether body contains text or
HTML mark up.
Attachments – Attachments. (If any)
ReplyTo – ReplyTo Email address.
SmtpClient class methods
Following are the methods of the SMTP class.
Host – SMTP Server URL (Gmail: smtp.gmail.com)
Port – Port Number of the SMTP sever (Gmail: 587)
EnableSsl – Specify whether your host accepts SSL Connections (Gmail: True)
UseDefaultCredentials – Set to True in order to allow authentication based on the Credentials of the Account used to send emails
Credentials – Valid login credentials for the SMTP server (Gmail: email address and password)
Exporting GridView to HTML and sending in email
When
SendEmail button is clicked, 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)
{
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())
{
//Read SMTP section from Web.Config.
SmtpSection smtpSection = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");
//Set the SMTP settings.
smtp.Host = smtpSection.Network.Host;
smtp.Port = smtpSection.Network.Port;
smtp.EnableSsl = smtpSection.Network.EnableSsl;
smtp.UseDefaultCredentials = smtpSection.Network.DefaultCredentials;
smtp.Credentials = new NetworkCredential(smtpSection.Network.UserName, smtpSection.Network.Password);
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)
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()
' Read SMTP section from Web.Config.
Dim smtpSection As SmtpSection = CType(ConfigurationManager.GetSection("system.net/mailSettings/smtp"), SmtpSection)
'Set the SMTP settings.
smtp.Host = smtpSection.Network.Host
smtp.Port = smtpSection.Network.Port
smtp.EnableSsl = smtpSection.Network.EnableSsl
smtp.UseDefaultCredentials = smtpSection.Network.DefaultCredentials
smtp.Credentials = New NetworkCredential(smtpSection.Network.UserName, smtpSection.Network.Password)
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