Monday, March 4, 2019

Store Email Template in .NET

Simple and Easy way to store/edit email templates in your .net project is to store them as string email body in a html file.
Step 1: Create a HTML mail message
2016-10-04-16_04_18-_c__users_hassan-muhammad_desktop_new-14-html-notepad
Step 1(a): You can introduce as many as variable in HTML template by any character scheme (@PARAMETER@ or #PARAMETER# or $$PARAMETER$$). 
Replace all names/variables with things like #VaraibleName#
The confirmation number is: <b>#confirmNumber#</b>
Step 2: Right click on web project and select Properties from right click menu
2016-10-04-16_08_58-recruiting-microsoft-visual-studio-administrator

 Step 3: Select Resources tab from left, choose Add Resource, select Add Existing File and choose appropriate html file

2016-10-04-16_24_57-recruiting-microsoft-visual-studio-administrator



Step 4: File will be visible in Resources display pan
2016-10-04-16_26_31-recruiting-microsoft-visual-studio-administrator


Step 5: For Server side C# code
You can refer to HTML email template from Properties.Resources.EmailTemplate
You can use it as a string. 
Replace the #PARAMETER# text with the actual values.


private void SendConfirmationEmail(string email, string confirmNumber)
{
    var emailBody = Properties.Resources.EmailTemplate.Replace("#confirmNumber#", confirmNumber);
    MailMessage message =
        new MailMessage
        {
            From =
            new MailAddress("Sender Email Address"),
            Subject = "Email Subject Content",
            Body = string.Format(emailBody),
            IsBodyHtml = true
        };
 
    message.To.Add(new MailAddress(email));
    SmtpClient client = new SmtpClient("11.111.111.11");
    client.Send(message);
}


0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More