Introduction:
In this article I will explain how to send html page 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. |
|||
|
|||
25 comments :
Super!! I got it.... You save my time.. Thanks..!
sir i am facing the problem when i use your code or logic problem is that
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 10.2.69.121:25
Please help me
I got the same error as Himanshu Sharma.Can u plzzz solve it??A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 10.2.69.121:25
can we send html file stored at any location ??? i mean if we can replace ~/htmlpage.html to fileupload1.tostring() or something ?????
i got the error message :
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 10.2.69.121:25.
Please help me.
I am speechless for your dedication ..........thankyousir
As always as simple and great like you.
Thank u for this valuable example thanks once again.
thank you so much..
thanks sir it is so helpful
Thanks sir. But i want to know that If the HTML File is uploaded by File Upload, how to use that file?
Got my answer:
i just passed this: StreamReader reader = new StreamReader(Server.MapPath(PostedFile.FileName));
thanks sir
regards
Arun kumar
cccccv
weell done
cv cvc
Why is he using an html page? stick everything in an aspx page with the script in the head section or use the code behind method..
thanks buddy for nice article for growing proffessional
what will bet the img src=? if images are stored on server... I can write server IP but IP may change sometime that need to change in code...
5 out of 5 for this superb article. I implemented your code and worked like a charm. Thanks for saving my time.
Sir, i have to work on the same requirement, my question is what if we have to print more than one mail id in the same html template page, then what would be the way to display it....
please provide me solution add print button in dended html page for printing my mail
Could not find file 'C:\Users\Tejas\Downloads\SendHTMLFileAsMailBody\HTMLPage.html'.when send file...
pls help me suresh
Dear suresh
It is most useful for us. but i'm trying a send a html page which is developed by using bootstrap.
Instead that i'm getting mail. but css and jquery not working in mail. please fix my bug...
Thanks in advance...
ytgyyyyyy
how to upload image into lh5.googleusercontent.com hosts?
Note: Only a member of this blog may post a comment.