How to Send Asp.Net Gridview Content or Records or Rows or Data through Email using C# VB.Net

Asp.Net GridView Control is used for report presentation. Sometimes we need to send those reports to admin level through email. Using Asp.Net we can achieve this easily. Here in this article I will explain with example “How to Send Asp.Net Gridview Content or Records or Rows or Data through Email using C# VB.Net“. If you follow my steps hope you can also send GridView content through Email to the client.

Objective/Outcome:
How to Send Asp.Net Gridview Content or Data through Email using Csharp VB.Net

To prepare the example write the below HTML:

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" 
    DataKeyNames="ID" HeaderStyle-BackColor="CornflowerBlue" HeaderStyle-Font-Bold="true" 
    HeaderStyle-ForeColor="White" CellPadding="5">
<Columns>
    <asp:BoundField DataField="Name" HeaderText="Name">
    </asp:BoundField>
    <asp:BoundField DataField="Address" HeaderText="Address">
    </asp:BoundField>
    <asp:BoundField DataField="Contact" HeaderText="Contact no">
    </asp:BoundField>
</Columns>
</asp:GridView>
<br />
    <asp:Label ID="lblStatus" runat="server" Text=""></asp:Label><br />
    <asp:Button ID="Button1" runat="server" Text="Send Mail GridView Content" 
        onclick="Button1_Click" />
</asp:Content>

Use Below Namespaces:
1. System.Data
2. System.Net.Mail
3. System.Text
4. System.IO

First Bind the GridView with Sample Data:
C# Code:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataTable dtSupplier = new DataTable("Supplier");
            dtSupplier.Columns.Add(new DataColumn("ID"));
            dtSupplier.Columns.Add(new DataColumn("Name"));
            dtSupplier.Columns.Add(new DataColumn("Address"));
            dtSupplier.Columns.Add(new DataColumn("Contact"));
            dtSupplier.Rows.Add("st0001", "S.R. Steel", "Uttara, Dhaka", "01711xxxxxx");
            dtSupplier.Rows.Add("ir0039", "Shadesh builders", "Rampura, Dhaka", "01711yyyyyy");
            dtSupplier.Rows.Add("cr0042", "Orchard confec.", "Shahabag, Dhaka", "01711zzzzzz");
            dtSupplier.Rows.Add("er0078", "Windblow", "Mirpur, Dhaka", "01711qqqqqq");
            dtSupplier.Rows.Add("bd0301", "Rahimkarim", "Badda, Dhaka", "01711oooooo");

            GridView1.DataSource = dtSupplier;
            GridView1.DataBind();
        }
    }

VB.Net Code:

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim dtSupplier As New DataTable("Supplier")
            dtSupplier.Columns.Add(New DataColumn("ID"))
            dtSupplier.Columns.Add(New DataColumn("Name"))
            dtSupplier.Columns.Add(New DataColumn("Address"))
            dtSupplier.Columns.Add(New DataColumn("Contact"))
            dtSupplier.Rows.Add("st0001", "S.R. Steel", "Uttara, Dhaka", "01711xxxxxx")
            dtSupplier.Rows.Add("ir0039", "Shadesh builders", "Rampura, Dhaka", "01711yyyyyy")
            dtSupplier.Rows.Add("cr0042", "Orchard confec.", "Shahabag, Dhaka", "01711zzzzzz")
            dtSupplier.Rows.Add("er0078", "Windblow", "Mirpur, Dhaka", "01711qqqqqq")
            dtSupplier.Rows.Add("bd0301", "Rahimkarim", "Badda, Dhaka", "01711oooooo")

            GridView1.DataSource = dtSupplier
            GridView1.DataBind()
        End If
    End Sub

Now Send GridView Content Through Email:
C# Code:

    protected void Button1_Click(object sender, EventArgs e)
    {
            MailMessage mailMsg = new MailMessage();

            // if you want to send HTML BODY then true for plain text body use false
            mailMsg.IsBodyHtml = true;
            mailMsg.Subject = "GridView Content";

            StringBuilder SB = new StringBuilder();
            StringWriter SW = new StringWriter(SB);
            HtmlTextWriter htmlTW = new HtmlTextWriter(SW);
            GridView1.RenderControl(htmlTW);

            mailMsg.Body = SB.ToString();
            mailMsg.From = new MailAddress("From Email");

            mailMsg.To.Add("To EMail");

            SmtpClient smtp = new SmtpClient("smtp.gmail.com");
            smtp.Port = 587;
            smtp.Credentials = new System.Net.NetworkCredential("Email Address", "your password");
            // If you use your smtp server then try first with blocking the below line.
            smtp.EnableSsl = true;
            smtp.Send(mailMsg);
            lblStatus.Text = "Mail Sent successfully !!";
    }

VB.Net Code:

    Protected Sub Button1_Click(sender As Object, e As EventArgs)
        Dim mailMsg As New MailMessage()

        ' if you want to send HTML BODY then true for plain text body use false
        mailMsg.IsBodyHtml = True
        mailMsg.Subject = "GridView Content"

        Dim SB As New StringBuilder()
        Dim SW As New StringWriter(SB)
        Dim htmlTW As New HtmlTextWriter(SW)
        GridView1.RenderControl(htmlTW)

        mailMsg.Body = SB.ToString()
        mailMsg.From = New MailAddress("From Email")

        mailMsg.[To].Add("To Email")

        Dim smtp As New SmtpClient("smtp.gmail.com")
        smtp.Port = 587
        smtp.Credentials = New System.Net.NetworkCredential("Email", "Password")
        ' If you use your smtp server then try first with blocking the below line.
        smtp.EnableSsl = True
        smtp.Send(mailMsg)
        lblStatus.Text = "Mail Sent successfully !!"
    End Sub

Don’t Forget to Add below Method:
C# Code:

public override void VerifyRenderingInServerForm(Control control) { }

VB.Net Code:

    Public Overrides Sub VerifyRenderingInServerForm(control As Control)
    End Sub

Note: If you are getting an error, first check your from mail address whether any notification is waiting for you or not. If yes then follow the notification email instruction.

Download Code Example C#        Download Code Example VB.Net

Posted in .Net, Asp.net, C#, Gridview, VB.Net

Leave a Reply

Your email address will not be published. Required fields are marked *

     

*