Asp.Net News
Free Tech Magazines
Asp.Net Web Hosting
Suggest Us
Link to Us
Feeds Subscription
Questions & Answers
Tips & Tricks
 Sending Emails with Dynamic Content
Posted by Moderator1 on 6/12/2007 12:37:58 AM Category: Asp.Net 2.0
Total Views : 97642
Add to my favorites
Email to friend
This article explains how to send email from with dynamic contents.
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 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,

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 '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.
 StreamReader sr=new StreamReader(“MyContent.htm”);
sr = File.OpenText(“MyContent.htm”);
string result = sr.ReadToEnd();
catch(Exception ex)

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.Subject = "Dynamic Content Email From "+ txtMyName.Text;
SmtpMail.SmtpServer = “your email server”;
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 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 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
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
Posted by raju on 5/12/2008 9:31:01 AM
hai how r u
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.
Posted by Swiss on 8/14/2008 9:01:46 AM
I too am using the code similar to the above. But am getting a lot of Formatting errors like some (! exclamations) in the middle and unnecessary spaces etc etc.. Can anyone please help me..? Thanks in Advance, Swiss
Posted by arni on 8/16/2008 9:12:20 PM
nice work man 10x
Posted by liza on 9/6/2008 6:03:36 PM
interesting site man
Posted by Balal on 9/15/2008 4:25:07 AM
Posted by Elango on 1/14/2009 10:16:44 PM
nice article for biginners tks buddy
Posted by Prakash on 1/31/2009 4:53:18 AM
Posted by milap on 2/11/2009 5:09:50 AM
it is very helpful article
Posted by vishal kaushik on 4/6/2009 6:33:13 AM
this code is really very helpful in understanding the flow of emails from the code.I got the evry part of the code and tried on my machine but i stuck at SmtpMail.SmtpServer = ""; I dont know what i have to write here as i dont have any server name.I also tried by opening inetmgr, whether there is any server name but there was nothing. So i want to know what i have to do now so that i can see the functionality of sending the mail from my code. Thanks In Advance
Posted by Stephen on 7/7/2009 3:40:28 AM
How can someone send a mail, without entering the password?
Posted by deepthi on 7/13/2009 5:21:48 AM
very much helpfull tank u
Posted by Pratap on 9/8/2009 7:24:59 AM
Its intereting
Posted by ED Kim on 4/8/2010 10:10:51 AM
Posted by karthik on 8/17/2010 10:43:31 AM
nice work..... Thanks and keep coding.
Posted by Jeff on 8/20/2010 9:51:53 AM
Did anyone find a solution for ALL the formatting issues like double quotes that are in the HTML file and thus cannot be handled in the String variable?
Posted by Michael on 12/28/2010 1:41:52 PM
Does this would work for someone clicking on linkbutton from my web page so to send his or her comment? Thank you.
Posted by Ankur Sharma on 3/6/2011 12:39:22 PM
This is really a helping one. But actually i want to send a mail after entering the email address in textbox and clicking on the submit button.. So if you help me in that way then it's helpfull a lot...
Posted by Harshad on 10/4/2011 4:16:53 AM
Hi, I have dynamic from (sender) in mail. have use SMTP with my domain. I am not able to send mail or not finding solution how to achieve it. Concider FROM is Dynamic, static i know how to do and it is working.
Posted by Prince Kumar Ravi on 10/22/2011 3:11:16 PM
I want to send the content from a form made in with C# as a code behind language to an email id. Haven't got anything for that. If anyone of you can help out in this please.
Posted by anjalesh on 1/16/2014 1:41:34 AM
Posted by anjalesh on 1/16/2014 2:39:13 AM
Posted by Dheeraj on 2/10/2016 11:21:45 AM
I want a web page that send a screenshot of the webpage to any email that's input in a box on the click of a button on that same webpage
Posted by shalini on 6/23/2016 5:21:18 AM
this blog is really informative and such a awful information it is really exclusive thanks for sharing.
 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:
Email Id:  
We never display your email id anywhere.
Comment/Question: Max. 500 letters
Sixty plus Ten Equals To
Sponsored by
Washington Technology
Introduction to Software Development