Saturday, October 27, 2007

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>

Code :

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);

No comments: