|
|
|
Sending Emails with Dynamic Content |
|
Posted by
Moderator1
on
6/12/2007 12:37:58 AM
|
Category:
Asp.Net 2.0 |
|
|
Total Views :
31257 |
|
Adding to your Favorites....
|
|
|
 |
|
|
|
|
|
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. |
|
|
|
|
|
|
|
|
|
|
|
|
|