In one of my post, I explained how to send E-mail in ASP.Net. Here I am explaining how to send gridview data in E-mail in ASP.Net using C#, VB.Net
In my previous posts, I explained Import Gmail contacts in ASP.Net, Export Gridview to PDF in ASP.Net with Image, Send mail in ASP.Net, Convert DataTable into List, Constructor Chainning in C#, Convert a Generic List to a Datatable, Get Property Names using Reflection in C#, Hard drive information using C#, Create Directory/Folder using C#, Check Internet Connection using C#, SQL Server Database BackUp using C# and some other articles related to C#, ASP.Net, jQuery, Java Script and SQL Server.
Fist of all lets create a table in our database and insert data into table for bind the gridview.
Now add the following namespaces in your code behind page
C#
Now write the following code on Click Event of button control.
C#
I hope this article will be helpful for you. I would like to have any feedback from you. Your valuable feedback, question, or comments about this article are always welcome.
In my previous posts, I explained Import Gmail contacts in ASP.Net, Export Gridview to PDF in ASP.Net with Image, Send mail in ASP.Net, Convert DataTable into List, Constructor Chainning in C#, Convert a Generic List to a Datatable, Get Property Names using Reflection in C#, Hard drive information using C#, Create Directory/Folder using C#, Check Internet Connection using C#, SQL Server Database BackUp using C# and some other articles related to C#, ASP.Net, jQuery, Java Script and SQL Server.
Fist of all lets create a table in our database and insert data into table for bind the gridview.
Now drag and drop a gridview and button control in your ASPX page and configure the DataSource for gridview as shown below-
ASPX Page<div> <asp:GridView ID="grdDemo" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" Width="500px"> <HeaderStyle CssClass="GridHeader" /> <Columns> <asp:BoundField DataField="EmpCode" HeaderText="Emp Code" SortExpression="EmpCode" /> <asp:BoundField DataField="FirstName" HeaderText="First Name" SortExpression="FirstName" /> <asp:BoundField DataField="LastName" HeaderText="Last Name" SortExpression="LastName" /> </Columns> </asp:GridView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DotNetWorldDemoConnectionString %>" SelectCommand="SELECT [FirstName], [LastName], [EmpCode] FROM [UserInfo]"></asp:SqlDataSource> </div> <asp:Button ID="btnSndMail" runat="server" Text="Send Mail" onclick="btnSndMail_Click" />
Now add the following namespaces in your code behind page
C#
using System.Net.Mail; using System.Text; using System.IO;VB.Net
Imports System.Net.Mail Imports System.Text Imports System.IO
Now write the following code on Click Event of button control.
C#
protected void btnSndMail_Click(object sender, EventArgs e) { //Create instance of MailMessage Class MailMessage msg = new MailMessage(); //Assign From mail address msg.From = new MailAddress("frommail@xyz.com"); //Set To mail address msg.To.Add(new MailAddress("tomail@abc.com")); //Set Subject of mail msg.Subject = "Send Gridview Data in Mail"; //Create Mail Body msg.Body = "Please check the follwoing data "; msg.Body += GetGrdiviewData(grdDemo); //for sending body as HTML msg.IsBodyHtml = true; //Create Instance of SMTP Class SmtpClient SmtpServer = new SmtpClient(); //Assign Host SmtpServer.Host = "smtp.gmail.com"; //Assign Post Number SmtpServer.Port = 587; //Setting the credential for authentiicate the sender SmtpServer.Credentials = new System.Net.NetworkCredential("mymail@gmail.com", "password"); //Enable teh Secure Soket Layer to Encrypte the connection SmtpServer.EnableSsl = true; //Sending the message SmtpServer.Send(msg); } public string GetGrdiviewData(GridView grd) { StringBuilder strBuild = new StringBuilder(); StringWriter strWrite = new StringWriter(strBuild); HtmlTextWriter html = new HtmlTextWriter(strWrite); grd.RenderControl(html); return strBuild.ToString(); } public override void VerifyRenderingInServerForm(Control control) { //Varify that control is render }VB.Net
Protected Sub btnSndMail_Click(sender As Object, e As EventArgs) 'Create instance of MailMessage Class Dim msg As New MailMessage() 'Assign From mail address msg.From = New MailAddress("frommail@xyz.com") 'Set To mail address msg.[To].Add(New MailAddress("tomail@abc.com")) 'Set Subject of mail msg.Subject = "Send Gridview Data in Mail" 'Create Mail Body msg.Body = "Please check the follwoing data " msg.Body += GetGrdiviewData(grdDemo) 'for sending body as HTML msg.IsBodyHtml = True 'Create Instance of SMTP Class Dim SmtpServer As New SmtpClient() 'Assign Host SmtpServer.Host = "smtp.gmail.com" 'Assign Post Number SmtpServer.Port = 587 'Setting the credential for authentiicate the sender SmtpServer.Credentials = New System.Net.NetworkCredential("mymail@gmail.com", "password") 'Enable teh Secure Soket Layer to Encrypte the connection SmtpServer.EnableSsl = True 'Sending the message SmtpServer.Send(msg) End Sub Public Function GetGrdiviewData(grd As GridView) As String Dim strBuild As New StringBuilder() Dim strWrite As New StringWriter(strBuild) Dim html As New HtmlTextWriter(strWrite) grd.RenderControl(html) Return strBuild.ToString() End Function Public Overrides Sub VerifyRenderingInServerForm(control As Control) 'Varify that control is render End SubOutput Mail
I hope this article will be helpful for you. I would like to have any feedback from you. Your valuable feedback, question, or comments about this article are always welcome.
No comments:
Post a Comment