My contemplations and diurnal novelties are publicized here


You can simply send an email using the template as an html using following code

private void SendEmail()
{
try
{
string subject = “Email Subject”;
AlternateView htmlView = GetEmailViewFromTemplateTemplate(“Wahab Hussain”);

System.Net.Mail.MailMessage Msg = new System.Net.Mail.MailMessage();
System.Net.NetworkCredential SmtpUser = new System.Net.NetworkCredential();
SmtpUser.UserName = “youraccount@gmail.com”;
SmtpUser.Password = “YourAccountPassword”;
Msg.To.Add(“youraccount@gmail.com”);

System.Net.Mail.MailAddress fromAddress = new System.Net.Mail.MailAddress(“youraccount@gmail.com”, “Your Name”);
Msg.From = fromAddress;

Msg.Subject = subject;
Msg.Body = null;
Msg.IsBodyHtml = true;
Msg.AlternateViews.Add(htmlView);

System.Net.Mail.SmtpClient SmtpServer = new System.Net.Mail.SmtpClient();
SmtpServer.Credentials = SmtpUser;
SmtpServer.Port = 587;
SmtpServer.EnableSsl = true;
SmtpServer.Host = “smtp.gmail.com”;

SmtpServer.Send(Msg);
}
catch (Exception ex)
{

}
}

private AlternateView GetEmailViewFromTemplateTemplate(string toName)
{
StringBuilder emailTemplate = new StringBuilder();
string filePath = Server.MapPath(“emailTemplate.htm”);

AlternateView htmlView = null;
if (System.IO.File.Exists(filePath))
{
emailTemplate = new StringBuilder(System.IO.File.ReadAllText(filePath));
emailTemplate.Replace(“<%Name%>”, toName);

htmlView = AlternateView.CreateAlternateViewFromString(emailTemplate.ToString(), null, “text/html”);

}

return htmlView;
}

protected void btnSendEmail_Click(object sender, EventArgs e)
{
SendEmail();
}

Here is the template

 <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt;
<html xmlns=”http://www.w3.org/1999/xhtml”&gt;
<head>
<title></title>
</head>
<body>
Hello <%Name%>
Congratulations!
How are you? you have received an email via email templating.

</body>
</html>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: