I have already covered how to send email in c#, you will be needing that also to understand this,
if u had gone through that then it is fine or else follow it here. [Send Email C#]
In this article I will explain how to send HTML Formatted emails. Now a day’s many websites sent newsletters, promotional emails. These emails containing Rich Text content and images
So let’s start with it.
Email Body HTML Template
Firstly you will need to build an HTML Template of the body which will have some placeholders which will be replaced with the actual content. Advantage of creating templates instead of building HTML using String Builder in code is that you can easily change the HTML of the template without changing the code.
<html>
<body style="color:grey; font-size:15px;">
<font face="Helvetica, Arial, sans-serif">
<div style="background-color: #ece8d4; width:600px; height:200px; padding:30px; margin-top:30px;">
<p>Hi {UserName},<p>
<p>You Have Been Added As A User In eProject Manager Application</p>
<p>
UserName : {LoginId}
Password : {Password}
<br>
<br/>
<p>Thank you</p>
</div>
</body>
</html>
Above you will notice in YELLOW I have created the following three placeholders in the HTML Email template.
{UserName} – Name of the recipient
{LoginId} – Id used for the login
{Password} – Password for login
We are now almost done. Now the only thing we need to do is replace the placeholders with their actual values and send the email.
Populating the HTML Formatted Body
On the click event handler of a button I will first read the HTML Template file and then replace the placeholders with their values.
public string PopulateBody(string userName, string loginid, string pass)
{
string body = string.Empty;
using (StreamReader reader = new StreamReader(Server.MapPath("~/EmailTemplate.htm")))
{
body = reader.ReadToEnd();
}
body = body.Replace("{UserName}", userName);
body = body.Replace("{LoginId}", loginid);
body = body.Replace("{Password}", pass);
return body;
}
With that the email template is complete.. you simply have to pass the return of this function to the email's message bode section.
View this link so you will be able to understand the sending email section too. [Send Email C#]
MailMessage message = new MailMessage();
message.IsBodyHtml = true;
message.Body = PopulateBody('Hudhaifa','hudhaifa93','123456789');
Now the email body will contain ur email template with your parameters passed..
Hope this is useful for the reader.
Any Problem,Suggestion or Help , Do not hesitate, Drop a mail.