Introduction:
Here I will explain how to send html page or file as email
body in asp.net using C# and VB.NET.
Description:
In previous article I explained Send Mail in asp.net, Send mail with images using gmail credentials, Send mail with attachment in asp.net and many articles relating to JQuery, asp.net, SQL Server etc. Now I will explain how to send html page as email body in asp.net using C# and VB.NET.
In previous article I explained Send Mail in asp.net, Send mail with images using gmail credentials, Send mail with attachment in asp.net and many articles relating to JQuery, asp.net, SQL Server etc. Now I will explain how to send html page as email body in asp.net using C# and VB.NET.
To implement this concept first create new web application >>
Right click on your application >> Select Add New item >> Select
HTML Page and click OK
Once HTML page added to your application open it and write the
following code in HTMLPage.htm page
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>HTML Page to Send Mail </title></head>
<body>
<img src =
"https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhJ3C-tQDXPIMmR_dS4yVjSxehkRh6cA3XmKuGynVFcv0my1TMzWis9ucLgUvpBhQ76zwFfKrp5SieMfZjZmiWdk8DCcL8rlv315aRPY8u3RjzO1zBros0Dm9NtVljyBYMtukeEmxuR01Q/"
/><br
/><br
/>
<div style =
"border-top:3px solid #EB5E00"> </div>
<table style="border:1px solid
#EB5E00">
<tr>
<td><b><span style =
"font-family:Arial;font-size:10pt">Admin:</span></b></td>
<td>$$Admin$$</td>
</tr>
<tr>
<td><b><span style =
"font-family:Arial;font-size:10pt">CompanyName:</span></b></td>
<td>$$CompanyName$$</td>
</tr>
<tr>
<td><b><span style =
"font-family:Arial;font-size:10pt">EMail:</span></b></td>
<td>$$Email$$</td>
</tr>
<tr>
<td><b><span style =
"font-family:Arial;font-size:10pt">Website:</span></b></td>
<td>$$Website$$</td>
</tr>
</table>
<p><span style =
"font-family:Arial;font-size:10pt">To know more about Aspdotnet-Suresh.com,
please visit - http://www.aspdotnet-suresh.com </span> </p>
<span style =
"font-family:Arial;font-size:10pt">Thanks</span>
<br />
<b><span style =
"font-family:Arial;font-size:10pt">Aspdotnet-Suresh</span></b>
</body>
</html>
|
Now open your Default.aspx page and
write the following code
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Send HTML page as email
body in asp.net</title>
</head>
<body>
<form id="form1"
runat="server">
<div>
<asp:Button ID="btnSend"
Text="Send
Mail" runat="server" onclick="btnSend_Click" />
</div>
</form>
</body>
</html>
|
Now add the following namespaces in
code behind
C# Code
using System;
using System.Net.Mail;
using System.IO;
using System.Configuration;
|
After add namespaces write the following code in code behind
protected void Page_Load(object
sender, EventArgs e)
{
}
protected void btnSend_Click(object
sender, EventArgs e)
{
SendHTMLMail();
}
// Method Which is used to Get HTML File and replace
HTML File values with dynamic values and send mail
public void SendHTMLMail()
{
StreamReader reader = new StreamReader(Server.MapPath("~/HTMLPage.htm"));
string readFile =
reader.ReadToEnd();
string myString = "";
myString = readFile;
myString = myString.Replace("$$Admin$$",
"Suresh Dasari");
myString = myString.Replace("$$CompanyName$$",
"Dasari Group");
myString = myString.Replace("$$Email$$",
"suresh@gmail.com");
myString = myString.Replace("$$Website$$",
"http://www.aspdotnet-suresh.com");
MailMessage Msg = new MailMessage();
MailAddress fromMail =
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 Mail with HTML
File";
Msg.Body = myString.ToString();
Msg.IsBodyHtml = true;
string sSmtpServer = "";
sSmtpServer = "10.2.69.121";
SmtpClient a = new SmtpClient();
a.Host = sSmtpServer;
a.Send(Msg);
reader.Dispose();
}
|
VB.NET Code
Imports System.Net.Mail
Imports System.IO
Imports System.Configuration
Partial Class VBSample
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As
Object, e As EventArgs)
End Sub
Protected Sub btnSend_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 reader As New StreamReader(Server.MapPath("~/HTMLPage.htm"))
Dim readFile As String =
reader.ReadToEnd()
Dim myString As String = ""
myString = readFile
myString = myString.Replace("$$Admin$$",
"Suresh Dasari")
myString = myString.Replace("$$CompanyName$$",
"Dasari Group")
myString = myString.Replace("$$Email$$",
"suresh@gmail.com")
myString = myString.Replace("$$Website$$",
"http://www.aspdotnet-suresh.com")
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 Mail with HTML
File"
Msg.Body = myString.ToString()
Msg.IsBodyHtml = True
Dim sSmtpServer As String = ""
sSmtpServer = "10.2.69.121"
Dim a As New SmtpClient()
a.Host = sSmtpServer
a.Send(Msg)
reader.Dispose()
End Sub
End Class
|
Demo
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
|
|||
0 comments :
Note: Only a member of this blog may post a comment.