Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

How to Send HTML Page As Email Body in Asp.Net Using C#.NET,VB.NET

Sep 18, 2012
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.

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">&nbsp;</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

Download Sample Code Attached




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.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

25 comments :

Anonymous said...

Super!! I got it.... You save my time.. Thanks..!

Unknown said...

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

ShireeshaKandukoori said...

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

darpan pathak said...

can we send html file stored at any location ??? i mean if we can replace ~/htmlpage.html to fileupload1.tostring() or something ?????

Anonymous said...

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.

Abhinavsingh993 said...

I am speechless for your dedication ..........thankyousir
As always as simple and great like you.

SRASEO said...

Thank u for this valuable example thanks once again.

Anonymous said...

thank you so much..

Unknown said...

thanks sir it is so helpful

Unknown said...

Thanks sir. But i want to know that If the HTML File is uploaded by File Upload, how to use that file?

Unknown said...

Got my answer:

i just passed this: StreamReader reader = new StreamReader(Server.MapPath(PostedFile.FileName));

akumar said...

thanks sir
regards
Arun kumar

Anonymous said...

cccccv

Rohit said...

weell done

Rohit said...

cv cvc

Michael Summers said...

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..

Unknown said...

thanks buddy for nice article for growing proffessional

Anonymous said...

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...

Anonymous said...

5 out of 5 for this superb article. I implemented your code and worked like a charm. Thanks for saving my time.

chetan said...

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....

Unknown said...

please provide me solution add print button in dended html page for printing my mail

tejas said...

Could not find file 'C:\Users\Tejas\Downloads\SendHTMLFileAsMailBody\HTMLPage.html'.when send file...
pls help me suresh

Unknown said...

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...

Unknown said...

ytgyyyyyy

Ram Babu said...

how to upload image into lh5.googleusercontent.com hosts?

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.