AspdotnetCodes.com
Voted 'Best ASP.NET Host' for 2008 – DiscountASP.NET
 
Articles Subscribe for our Articles Updates
Books
Resources
Downloads
Free Tech Magazines
Archives
Softwares
Newsletter
Suggest Us
Link to Us
 Sending Emails with Dynamic Content
Posted by Moderator1 on 6/12/2007 12:37:58 AM Category: Asp.Net 2.0
Total Views : 18452
Add to my favorites
Email to friend
  
Introduction
This article explains how to send email from asp.net with dynamic contents.
Description
There are lots and lots of requirements come in our day to day life of web application development. One thing which we do most is sending notification emails to users of our website. In case, if your website is an e-commerce system, then you have to send emails such as customer registration confirmation emails, invoice emails and even payment confirmation emails. So I took my time to explain how to send emails with dynamic contents through asp.net 2.0.
System.Web.Mail Namespace

The System.Web.Mail namespace contains classes that enable you to construct and send messages using the CDOSYS (Collaboration Data Objects for Windows 2000) message component. The mail message is delivered either through the SMTP mail service built into Microsoft Windows 2000 or through an arbitrary SMTP server. The classes in this namespace can be used from ASP.NET or from any managed application.
System.IO Namespace
The System.IO namespace contains types that allow reading and writing to files and data streams, and types that provide basic file and directory support.
These two namespaces plays a major role in sending emails with dynamic content. I want the message of the email to like
Hi <yourfriendname>,

Thank you for sending this email. See you soon.

Good Bye,

<yourname>
<dateyousendthisemail>
So open a web project in Microsoft Visual Studio, obviously you can see a Default.aspx page. Add one html file named MyContent.html. Copy and paste the above message to the html file. Then come to Default.aspx file, add two textboxes and rename it as txtMyName and txtFriendName. Add a button and rename it as cmdSendEmail and change the Button’s Text to Send Email.

Now in Default.aspx.cs, include those two namespaces as follows.
using System.Web.Mail;
using System.IO;
Create an object for StreamReader class by assigning the html file path. Then open the file with File.OpenText method. So the content of the html file will be read and stored in the result variable. Now you can find and replace the content by the values in the Textboxes using the asp.net 'Replace' method and store the resultant content to another variable called MessageBody. So the email is ready with dynamic contents.

Now we create an object to the MailMessage as mail. Set the BodyFormat property to MailFormat.Html. This tells the mail object, you are going to send a Html based content message. Then set the From, To, Body and the Subject properties of the mail object. Point the SmtpMail.SmtpServer to your mail server. In most cases, it will be 'localhost'. The last line of the code, triggers the email from your application to the target mail server.
try
{
 StreamReader sr=new StreamReader(“MyContent.htm”);
sr = File.OpenText(“MyContent.htm”);
string result = sr.ReadToEnd();
sr.Close();
}
catch(Exception ex)
{
Response.Write(ex.Message);
return;
}


string MessageBody=result.Replace("<YourFriendName>", txtFriendName.Text);
MessageBody=MessageBody.Replace("<YourName>", txtMyName.Text);
MessageBody= MessageBody.Replace("<DateYouSendThisEmail>", DateTime.Today.ToString());
MailMessage mail = new MailMessage();
mail.Body = MessageBody;
mail.BodyFormat = MailFormat.Html;
mail.From = “YOUR EMAIL ID”;
mail.To = “YOUR FRIEND’S EMAIL ID”;
mail.Subject = "Dynamic Content Email From "+ txtMyName.Text;
SmtpMail.SmtpServer = “your email server”;
SmtpMail.Send(mail);
So you can use the above block of code to send emails from your web applications. You can design any type of html content and replace the necessary dynamic values from the code-behind. Hope this small article will help you lot.
 
Viewer's Comments
Posted by Easy on 7/20/2007 9:30:01 PM
very easy to do this. thanks
 
Posted by s on 8/6/2007 1:16:22 AM
hi, how ru?
 
Posted by s on 8/6/2007 1:16:51 AM
its very useful. thanx
 
Posted by saurabh on 8/6/2007 1:17:32 AM
very efficient. thanx for the code
 
Posted by wynaf kaye on 9/1/2007 10:28:40 AM
yxokqugb toweib qgrsbfack yezng povlzgead fzyx lfpicaht
 
Posted by etert on 11/8/2007 3:34:15 AM
rrrewr
 
Posted by Cnu on 12/17/2007 4:04:09 AM
it will shows good implimentations
 
Posted by Himanshu on 1/16/2008 3:28:52 AM
Very nice doc to helpful to others
 
Posted by sdfsdff on 1/22/2008 10:58:04 AM
sdfsadfsdaf
 
Posted by sdfsdff on 1/22/2008 10:58:21 AM
tests fasdf asdfera dsfwerwer
 
Posted by James on 1/25/2008 9:32:18 AM
I implemented exactly as you specified and received so many formatting errors I could not count them all. You are using C#? It does not pick up the double quotes in your code. Do you have a workable solution? I'm a newbie to the VS dot Net. Thank you, James
 
Posted by prashanth on 2/5/2008 7:55:51 AM
prashanth.kenchanahallishivanna@logicacmg.com
 
Posted by Radhika on 3/10/2008 6:10:25 AM
hey Nice code. Useful One.Thanks and keep coding.
 
Posted by kamce on 5/8/2008 4:38:02 PM
nice
 
Posted by raju on 5/12/2008 9:31:01 AM
hai how r u
 
Posted by fiznEVkKRBKd on 5/17/2008 10:35:14 AM
b6OB6V
 
Posted by arni on 5/19/2008 5:59:45 PM
nice site dude
 
Posted by arni on 5/19/2008 8:26:16 PM
hi nice site thx
 
Posted by Zaki Ahmad on 6/18/2008 12:00:04 AM
Sorry , Buddy i have'nt understand this code the problem is the code which you writer from try up SmtpMail.Send(mail); Where it has to code.
 
 Rating & Comments
A word 'Excellent' means lot to the author of this article. You can give comments about this article but not the author.
Rate this Article:   
Name:
Email Id:  
We never display your email id anywhere.
Comment/Question: Max. 500 letters
 
Sponsored by