Hi nedash,
Check the below sample.
HTML
EmailTemplate.htm
<!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>
<title></title>
</head>
<body>
<div style="border-top: 3px solid #22BCE5">
</div>
<span style="font-family: Arial; font-size: 10pt">Hello <b>{UserName}</b>,<br />
<br />
<br />
<br />
<br />
{Description}
<br />
<br />
Thanks
<br />
</body>
</html>
CS.aspx
<asp:GridView ID="GridView3" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Download" ItemStyle-Width="24px" ItemStyle-VerticalAlign="Middle">
<ItemTemplate>
<asp:HyperLink ID="HPtemporary" runat="server" NavigateUrl='<%#Eval("DownLoadToken","http://Site.com/downloads/{0}")%>'><%#Eval("DownLoadToken","http://site.com/downloads/{0}")%></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:Button ID="btnSend" runat="server" Text="Send" OnClick="SendEmail" />
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] { new DataColumn("DownLoadToken") });
dt.Rows.Add("Test.Pdf");
dt.Rows.Add("Test.xls");
GridView3.DataSource = dt;
GridView3.DataBind();
}
}
private string PopulateBody(string userName, string description)
{
string body = string.Empty;
using (StreamReader reader = new StreamReader(Server.MapPath("~/EmailTemplate.htm")))
{
body = reader.ReadToEnd();
}
body = body.Replace("{UserName}", userName);
body = body.Replace("{Description}", description);
return body;
}
protected void SendEmail(object sender, EventArgs e)
{
string body = this.PopulateBody("John", GetGridview(GridView3));
this.SendHtmlFormattedEmail("recipient@gmail.com", "New article published!", body);
}
public string GetGridview(GridView gridView)
{
string sr = "";
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter hw = new HtmlTextWriter(sw))
{
gridView.RenderControl(hw);
sr = sw.ToString();
}
}
return sr.ToString();
}
public override void VerifyRenderingInServerForm(Control control)
{
}
private void SendHtmlFormattedEmail(string recepientEmail, string subject, string body)
{
using (MailMessage mailMessage = new MailMessage())
{
mailMessage.From = new MailAddress(ConfigurationManager.AppSettings["UserName"]);
mailMessage.Subject = subject;
mailMessage.Body = body;
mailMessage.IsBodyHtml = true;
mailMessage.To.Add(new MailAddress(recepientEmail));
SmtpClient smtp = new SmtpClient();
smtp.Host = ConfigurationManager.AppSettings["Host"];
smtp.EnableSsl = Convert.ToBoolean(ConfigurationManager.AppSettings["EnableSsl"]);
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = ConfigurationManager.AppSettings["UserName"];
NetworkCred.Password = ConfigurationManager.AppSettings["Password"];
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = int.Parse(ConfigurationManager.AppSettings["Port"]);
smtp.Send(mailMessage);
}
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs)
If Not Me.IsPostBack Then
Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn() {New DataColumn("DownLoadToken")})
dt.Rows.Add("Test.Pdf")
dt.Rows.Add("Test.xls")
GridView3.DataSource = dt
GridView3.DataBind()
End If
End Sub
Private Function PopulateBody(userName As String, description As String) As String
Dim body As String = String.Empty
Using reader As New StreamReader(Server.MapPath("~/EmailTemplate.htm"))
body = reader.ReadToEnd()
End Using
body = body.Replace("{UserName}", userName)
body = body.Replace("{Description}", description)
Return body
End Function
Protected Sub SendEmail(sender As Object, e As EventArgs)
Dim body As String = Me.PopulateBody("John", GetGridview(GridView3))
Me.SendHtmlFormattedEmail("recipient@gmail.com", "New article published!", body)
End Sub
Public Function GetGridview(gridView As GridView) As String
Dim sr As String = ""
Using sw As New StringWriter()
Using hw As New HtmlTextWriter(sw)
gridView.RenderControl(hw)
sr = sw.ToString()
End Using
End Using
Return sr.ToString()
End Function
Public Overrides Sub VerifyRenderingInServerForm(control As Control)
End Sub
Private Sub SendHtmlFormattedEmail(recepientEmail As String, subject As String, body As String)
Using mailMessage As New MailMessage()
mailMessage.From = New MailAddress(ConfigurationManager.AppSettings("UserName"))
mailMessage.Subject = subject
mailMessage.Body = body
mailMessage.IsBodyHtml = True
mailMessage.[To].Add(New MailAddress(recepientEmail))
Dim smtp As New SmtpClient()
smtp.Host = ConfigurationManager.AppSettings("Host")
smtp.EnableSsl = Convert.ToBoolean(ConfigurationManager.AppSettings("EnableSsl"))
Dim NetworkCred As New System.Net.NetworkCredential()
NetworkCred.UserName = ConfigurationManager.AppSettings("UserName")
NetworkCred.Password = ConfigurationManager.AppSettings("Password")
smtp.UseDefaultCredentials = True
smtp.Credentials = NetworkCred
smtp.Port = Integer.Parse(ConfigurationManager.AppSettings("Port"))
smtp.Send(mailMessage)
End Using
End Sub
Screenshot