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

Send HTML File or Page as Email Body in Asp.net using C#, VB.NET

May 11, 2015
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.

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://lh5.googleusercontent.com/_B28NJpJ61hA/TdgnS7lh7mI/AAAAAAAAAi4/oLTicIRgEIw/FinalLogo.png" /><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

0 comments :

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.