Code Rewind
When its time to find, Hit Rewind
                 
 
 
Remember Me  
Recover Password
 
 Advanced Search
 
 Add to IE Search 
 
 
> Development > Web Development > Languages > ASP.NET => How to send emails using HTML Templates in ASP.NET
 

Available Files:
You must be signed in to download files.
You can Sign Up here if you are not a member.
Author: Binoj Daniel
Level: 4
Date Posted: 16-Dec-2008 (16:13)
Last Updated: 16-Dec-2008 (16:25)
Views: 1,581
Favorited: 0
Votes:
  -  1 votes
Your vote:
Click to rate Click to rate Click to rate Click to rate Click to rate
(5 Stars)
Rating: 5.00 out of 5


Summary: Have you ever faced a need to send personalized email using preformated HTML templates. If yes then this article will give you a better idea on how to use HTML templates in ASP.NET to send custom emails.


Introduction:
Did you ever felt the need of using a HTML template with place holders for variable fields to be replaced to send personalized emails to each user? If yes, then this article will give you a better idea on how to use the System.Web.Mail namespace in .NET.

You can download the complete source files also attached with this article.


Elaboration:


Here is what you need to do to get started.

- Create a generic HTML template file with the required variables. In this example we will take our file name as ForgotPassword.html and the variable to replace while sending personalized mails would be @add_username & @add_password.

Below is the source for the HTML template file used in this article.


<!

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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Forgot Password</title>
</
head>
<
body>
<div>
<table width="600px">
<tr><td style="height: 230px; width: 600px;"><p><span>Dear <strong><span>@add_username</span></strong>,</span></p>
<p><span></span></p><p style="margin: 0in 0in 0pt">--------------------------------------------------------------------------------------------------<br />
Here are the login details of your account.</p>
<p style="margin: 0in 0in 0pt">&nbsp;</p>
<p style="margin: 0in 0in 0pt">Username :- <b>@add_username</b>
<br />Password : <b>@add_password</b><br />
--------------------------------------------------------------------------------------------------&nbsp;<br />
<span><br />Best Regards,<br />
www.abc.com</span></p></td>
</tr>
</table>
</div>
</
body>
</
html>

Here are the web.config settings used in this sample article.
<
appSettings>
<add key="TemplatePath" value="C:/ABC_Site/Templates/"/>
<add key="SmtpServer" value="mail.abc.com"/>
<
add key="SmtpUser" value="abc@abc.com>
<
"/add key="SmtpPass" value="*******"/>
</appSettings>

 //This is the method to send email, you can call this method on the Button Click event inside an ASP.NET Web Form.

public string SendPasswordMail(string user, string pwd)
{
try
{
SmtpMail.SmtpServer = System.Configuration.ConfigurationManager.AppSettings["SmtpServer"]; //Read all the configuration values from web.config.
MailMessage oMessage = new MailMessage();
oMessage.Fields[
"http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"] = 1;
oMessage.Fields[
"http://schemas.microsoft.com/cdo/configuration/sendusername"] = System.Configuration.ConfigurationManager.AppSettings["SmtpUser"];
oMessage.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"] = System.Configuration.ConfigurationManager.AppSettings["SmtpPass"];
oMessage.BodyFormat = MailFormat.Html;
oMessage.From = System.Configuration.
ConfigurationManager.AppSettings["SmtpUser"].ToString();
oMessage.Subject =
"Password Reminder Notification";
oMessage.To = user;
oMessage.Headers.Add(
"content-type", "text/html;");
string str = "";
string FilePath = System.Configuration.ConfigurationManager.AppSettings["TemplatePath"].ToString();

if (File.Exists(FilePath + "ForgotPassword.htm"))
{
FileStream f1 = new FileStream(FilePath + "ForgotPassword.htm", FileMode.Open);
StreamReader sr = new StreamReader(f1);
str = sr.ReadToEnd();
str = str.Replace("@add_username", user); //Replace the values from DB or any other source to personalize each mail.
str = str.Replace(
"@add_password", pwd);
f1.Close();
}
oMessage.Body = str;
SmtpMail.Send(oMessage);
return "Mail Sent Successfully!";
}
catch (Exception ex)
{
return "Error: Mail cannot be sent!";
}

Version Tracking:

Version 1.0


 
KeyWords: Generic / None (Generic / None);Generic / None (Generic / None);Generic / None (Generic / None);
CR Suggested ASP.NET Articles
 
 
Most Recent Views
 
Web Session Management by Guest (09-Sep-2010 08:07)
Multi-Threading in .NET by Guest (09-Sep-2010 07:58)
Use of Templates in C++ by Guest (09-Sep-2010 07:50)
Working with Attributes in .NET by Guest (09-Sep-2010 07:42)
How to use Namespace Alias Qualifier - (global) by Guest (09-Sep-2010 07:30)
 
 Messages: 0, Topics: 0. Post New Message Please login to post a message...
  View
Items per page
Message since
  TOPIC
AUTHOR
VIEWS
REPLIES
LAST POST
No messages boards...

Post New Message      
General Comment News / Info Question Answer Joke / Game Admin Answer
SEARCH ON FORUM
 
 
   
 
 
TOP USERS
 
No top users
 
 
TOP DISCUSSIONS
 
No popular discussions
 
 
SPONSORED ADS
 
 
 
 



 
Registered Members: 1622
Now Browsing: 8
 
Subscribe to newsletter
 
 

 
TheBusinessXP
IT2Max
AtHomeTution
RapidConvert
 








 
About Us  |  Contact Us   |  Privacy Policy  |  Legal Notice  |  Terms and Conditions  |  Help   |  Browse CR   |  Articles  |  Webcasts  |  Ask an Expert   |  Message Boards   |  Downloads  |  Open Arena   |  FAQ | DaniWeb | GetAHelpdesk | ProgTalk
Copyright © 2007 CodeRewind.com. All rights reserved
designed by IT2Max, INC.
 
Execution Time: 2.02 sec