How to send gridview content in email using ASP.NET?
In this article I am going to explain about how to send gridview content as email to user. This technique is used for your in various situation. I have use two techniques here to send email.
Description :
Most of the time we need to send email gridview content in our project requirement so I have write code for that requirement here. Here I have explained in two ways how to send gridview content as email
1) Send gridview content as an email excel attachment
2) Send gridview content directly in the email bodyClient side
In the client side I have used one gridview control and the two buttons to test send gridview content.
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">
<RowStyle BackColor="#E3EAEB" />
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#7C6F57" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
<br />
<br />
<asp:Button ID="btnExcelSend" runat="server" Text="Send GridView Content attached Excel file in email"
OnClick="btnExcelSend_Click" /> <br />
<asp:Button ID="btnGvEmail" runat="server" Text="Send GridView Content directly in email body"
OnClick="btnGvEmail_Click" />
<br />Server side
In the server side I have send email in two different ways
using System.Data;
using System.Net;
using System.Net.Mail;
public partial class _Default : System.Web.UI.Page
{
DataTable dt = new DataTable();
DataRow dr;
string frmid="fromid@gmail.com"; //change from id here
string frmpwd="frmpwd"; //change password here
string toid = "toid@anydomain.com"; //change toid here
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindGrid();
}
}
void BindGrid()
{
dt.Columns.Add("Empno");
dt.Columns.Add("Empname");
dt.Columns.Add("Salary");
dr = dt.NewRow();
dr["Empno"] = "101";
dr["Empname"] = "Ravindran";
dr["Salary"] = "25000";
dt.Rows.Add(dr);
if (dt.Rows.Count > 0)
{
Session["source_table"] = dt;
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
public override void VerifyRenderingInServerForm(Control control)
{
}
protected void btnExcelSend_Click(object sender, EventArgs e)
{
//here i am sending gridview content as excel attachment in email
ExcelEmail();
}
void ExcelEmail()
{
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
GridView1.RenderControl(htw);
string renderedGridView = sw.ToString();
//write file in to disk
System.IO.File.WriteAllText(Server.MapPath("temp.xls"), renderedGridView);
try
{
MailMessage msg = new MailMessage(frmid, toid, "Test GridView Attachment", "Mail_Body");
SmtpClient mailClient = new SmtpClient("smtp.gmail.com", 587);
NetworkCredential NetCrd = new NetworkCredential(frmid,frmpwd);
//Attach that excel sheet
msg.Attachments.Add(new Attachment(Server.MapPath("temp.xls")));
mailClient.UseDefaultCredentials = false;
mailClient.Credentials = NetCrd;
mailClient.EnableSsl = true;
mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
mailClient.Send(msg);
msg.Dispose();
//Delete that temp file after sent
if (System.IO.File.Exists(Server.MapPath("temp.xls")))
{
System.IO.File.Delete(Server.MapPath("temp.xls"));
}
}
catch (Exception ex)
{
}
}
protected void btnGvEmail_Click(object sender, EventArgs e)
{
//here i am sending gridview content directly as an email
GvEmail();
}
void GvEmail()
{
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
GridView1.RenderControl(htw);
string renderedGridView = sw.ToString();
try
{
MailMessage msg = new MailMessage(frmid, toid, "Test GridView Content", renderedGridView);
SmtpClient mailClient = new SmtpClient("smtp.gmail.com", 587);
NetworkCredential NetCrd = new NetworkCredential(frmid, frmpwd);
msg.IsBodyHtml = true;
mailClient.UseDefaultCredentials = false;
mailClient.Credentials = NetCrd;
mailClient.EnableSsl = true;
mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
mailClient.Send(msg);
msg.Dispose();
}
catch (Exception ex)
{
}
}
}
Source code:
Client Side: ASP.NET
Code Behind: C#Conclusion
I hope this code snippet is help you to know about how to send gridview content as email.
Thank you very much keep on posting like this.