Skip to main content

Creating dynamic email templates using C# and Office Outlook



It is quite common for many applications to send automated email notifications. Couple of months ago, I have worked on improving our old email template format to make it more user friendly.

In this tutorial I will walk you though regarding how I took advantage of Microsoft Outlook to quickly generate custom email template and later using the html template for building an automated custom email application using C#.

Steps:

  • Creating Templates: Using the rich text editor support  in Outlook create a nicely formatted email. Use placeholder text for the values you like to change dynamically based on your task completion status.

    To keep this tutorial simple, I have created a  simple table with placeholder text inside the third bracket [place holder text]. However, you can use anything supported by outlook editor.
Figure: Email Template

  • Getting HTML code: Send the created email to your own address. After that, open the sent email and right click to view source. It will display the HTML source of the email body with all the formatting you have made. Copy the HTML source to a separate file and save it as EmailTemplate.html

HTML Source of the email
C# Application
Create a C# Console application and add the  EmailTemplate.html file  to your project
  • Now in the  C# console application you can read all text from the HTML file and replace the placeholder content with the value of your corresponding variable.
    • For example, in the following code sample I am replacing the placeholder text in the HTML with my own value.
      private string GenerateEmailBody(JobLoadParam jlp)  
         {  
           string emailBody = string.Empty;  
           try  
           {  
             string strHTML = File.ReadAllText("EmailTemplate.html");  
             strHTML = strHTML                 
                     .Replace("[SourceFileName]", jlp.SourceFileName)  
                     .Replace("[SourceFileNetworkPath]", jlp.SourceFileNetworkPath)  
                     .Replace("[ExceptionFileName]", jlp.ExceptionFileName)  
                     .Replace("[ExceptionNetworkPath]", jlp.ExceptionFileNetworkPath)  
                     .Replace("[TrackingReferenceCode]", jlp.TrackingReferenceCode);  
             emailBody = strHTML;  
           }  
           catch (Exception ex)  
           {  
             System.Diagnostics.EventLog.WriteEntry(ex.Source, ex.Message, System.Diagnostics.EventLogEntryType.Error);  
           }  
           return emailBody;  
         }  


  • After that, you can set the updated html text from above as Body of the MailMessage and can send the email using SMTP client. Make sure you have the IsBodyHtml property of the MailMessage object set to true.

 MailAddress to = new MailAddress("email@domain");      
 MailAddress from = new MailAddress("email@domain");  
 MailMessage mail = new MailMessage(from, to);      
 mail.Subject = "Test email";  
 mail.IsBodyHtml = true;  
 mail.Body = GenerateEmailBody(jlp);  
  
SmtpClient smtp = new SmtpClient();  
smtp.Host = "your host address";  
smtp.Port = "port";  
smtp.Send(mail);  
mail.Dispose();   

Cheers! Your user will be very happy.

Comments

Unknown said…

Hello friends, its wonderful paragraph on the topic of teachingand completely explained, keep it up all the time. facebook login
Abu Raihan said…
By taking a dynamic content block in your email copy, your subscribers get the matching email but the copy can have separate images, product offers, and even the call-to-actions can be reformed based on their customer personas. Without any confusion the esignmodo email builder will be your right choice for your email marketing.
Zohurul Alam said…

i am really lucky to read this blog. i have got a lot of information from here. it is my good luck that
i read this blog. thank you very much writer.
for more information Office 365 Email Signature
Charli james said…
Thanks for providing recent updates regarding the concern, I look forward to read more. Email templates google workspace

Popular posts from this blog

Persian Music - Homayoon - Nemitonam English lyrics

I love this song. I have requested one of my Persian friend to translate it for me and she did really nice job.. I am sure you will love it.. " "The person who was the only person I had, was the only refuge of my lonely heart, Left me alone and went from my side I am restless form the pain of her separation … I thought she stays with me sings love song for me I thought she understand my words I didn’t know she’s unkind … Though gone, but still I am full of her love Her thought is always with me Wherever I go, she is in front of my eyes, in front of my eyes I want to stand out find a way to reduce my pain of her separation But it is not possible, There is no way I cannot bear I cannot bear The person who was the only person I had, was the only refuge of my lonely heart, Left me alone and went from my side I am restless form the pain of her separation … I thought she stays with me sings love song for me I thought she understand my words I didn’t know she’s u

Why using XOR might not be a good hash code implementation?

Using XOR for computing hash codes works great for most of the cases specially when order of computation does not matter. It also has the following benefits: XOR has the best bit shuffling properties of all bit-operations and provides better distributions of hash values. It is a quick single cycle operation in most computer  Order of computation does not matter. i.e. a^b = b^a However, if ordering of elements matter then it is often not a good choice. Example For simplicity consider you have a class with two string properties named Prop1 and Prop2  and your GetHashCode returns the xor of their hash code. It will work fine for most of the cases except cases where same values are assigned to different properties. It will generate same hash-code i.e. collision in that case as can be seen in the below example . However, using the modified approach as recommenced by Joshua Bloch's in Effective Java which uses prime multiplication and hash chaining provides more unif