Demo-:
Design Code-:
Asp.net Code Behind-:
VB.net Code-:
Design Code-:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SendGridEmail.aspx.cs" Inherits="SendGridviewWithEmail" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1">
<title>Send Mail Gridview Asp.net </title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvUserInfo" runat="server">
<HeaderStyle BackColor="#6699FF" Font-Bold="true" ForeColor="White" />
</asp:GridView>
</div><br /><br />
<asp:Button ID="btnSendMail" runat="server" Text="Send Gridview As Mail" onclick="btnSendMail_Click" />
</form>
</body>
</html>
Asp.net Code Behind-:
using System;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Net.Mail;
using System.Text;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class SendGridEmail: System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridview();
}
}
// Bind Databse with Gridview
protected void BindGridview()
{
SqlConnection con = new SqlConnection("Data Source=IT-HP\\sqlexpress;Initial Catalog=Employee;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand("SELECT TOP 10 UserName,FirstName,LastName,Location FROM UserInformation", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
gvUserInfo.DataSource = ds;
gvUserInfo.DataBind();
}
protected void btnSendMail_Click(object sender, EventArgs e)
{
SendHTMLMail();
}
// Get HTML File and replace HTML File values with values and send mail
public void SendHTMLMail()
{
MailMessage Msg = new MailMessage();
MailAddress fromMail = new MailAddress("rahulupadhyay010@gmail.com");
// Sender e-mail address.
Msg.From = fromMail;
// Recipient e-mail address.
Msg.To.Add(new MailAddress("Aspdotnetstudio@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;
string sSmtpServer = "";
sSmtpServer = "smtp.gmail.com";
SmtpClient a = new SmtpClient();
a.Host = sSmtpServer;
a.EnableSsl = true;
a.Send(Msg);
}
// This Method is used to render gridview control
public string GetGridviewData(GridView gv)
{
StringBuilder strBuilder = new StringBuilder();
StringWriter strWriter = new StringWriter(strBuilder);
HtmlTextWriter htw = new HtmlTextWriter(strWriter);
gv.RenderControl(htw);
return strBuilder.ToString();
}
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
}
VB.net Code-:
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 VBSendGridviewMail
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal 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=IT-HP\\sqlexpress;Initial Catalog=Employee;Integrated Security=True")
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(ByVal sender As Object, ByVal 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("rahulupadhyay010@gmail.com")
' Sender e-mail address.
Msg.From = fromMail
' Recipient e-mail address.
Msg.[To].Add(New MailAddress("Aspdotnetstudio@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 = "smtp.gmail.com"
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(ByVal 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(ByVal control As Control)
' Verifies that the control is rendered
End Sub
End Class
0 comments:
Post a Comment