How to Send Gridview in Email Body in Asp.Net Using VB.NET

Rajesh081725
Posted by Rajesh081725 under VB.NET category on | Points: 40 | Views : 3754
To implement this concept design your aspx page like this



<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1">
<title>send gridview in email body in asp.net using C#,VB.NET</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvUserInfo" runat="server">
<HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
</asp:GridView>
</div>
<asp:Button ID="btnSendMail" runat="server" Text="Send Gridview As Mail" onclick="btnSendMail_Click" />
</form>
</body>
</html>




Imports System.Web
Imports System.Data.SqlClient
Imports System.Data
Imports System.Net.Mail
Imports System.Text
Imports System.IO
Imports System.Web.UI
Imports System.Web.UI.WebControls


Partial Class VBCodeSendGridviewMail
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack Then
BindGridview()
End If
End Sub


' This method is used to bind gridview from database


Protected Sub BindGridview()
Dim con As New SqlConnection("Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB")
con.Open()
Dim cmd As New SqlCommand("SELECT TOP 10 UserName,FirstName,LastName,Location FROM UserInformation", con)
Dim da As New SqlDataAdapter(cmd)
Dim ds As New DataSet()
da.Fill(ds)
gvUserInfo.DataSource = ds
gvUserInfo.DataBind()
End Sub
Protected Sub btnSendMail_Click(sender As Object, e As EventArgs)
SendHTMLMail()
End Sub


' Method Which is used to Get HTML File and replace HTML File values with dynamic values and send mail
Public Sub SendHTMLMail()
Dim Msg As New MailMessage()
Dim fromMail As New MailAddress("administrator@aspdotnet-suresh.com")
' Sender e-mail address.
Msg.From = fromMail
' Recipient e-mail address.
Msg.[To].Add(New MailAddress("suresh@gmail.com"))
' Subject of e-mail
Msg.Subject = "Send Gridivew in EMail"
Msg.Body += "Please check below data <br/><br/>"
Msg.Body += GetGridviewData(gvUserInfo)
Msg.IsBodyHtml = True
Dim sSmtpServer As String = ""
sSmtpServer = "10.2.160.101"
Dim a As New SmtpClient()
a.Host = sSmtpServer
a.EnableSsl = True
a.Send(Msg)
End Sub
' This Method is used to render gridview control
Public Function GetGridviewData(gv As GridView) As String
Dim strBuilder As New StringBuilder()
Dim strWriter As New StringWriter(strBuilder)
Dim htw As New HtmlTextWriter(strWriter)
gv.RenderControl(htw)
Return strBuilder.ToString()
End Function
Public Overrides Sub VerifyRenderingInServerForm(control As Control)
' Verifies that the control is rendered
End Sub
End Class

Comments or Responses

Login to post response