Monday, 15 July 2013

How To- Send Gridview Data in Email in ASP.Net using C#, VB.Net

How To- Send Gridview Data in Email in ASP.Net using C#, VB.Net



Fist of all lets create a table in our database and insert data into table for bind the gridview.

Send Gridview Data in Email in ASP.Net using C#, VB.Net

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("mdubey587@gmail.com");
            //Set To mail address
            msg.To.Add(new MailAddress("mandeep.mca2007@gmail.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("mdubey587@gmail.com", "manishdivya");
            //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("mdubey587@gmail.com")
 'Set To mail address
 msg.[To].Add(New MailAddress("mandeep.mca2007@gmail.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("mdubey587@gmail.com", "manishdivya")
 '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 Sub
Output Mail
Send Gridview Data in Email in ASP.Net using C#, VB.Net

No comments:

Post a Comment