How to send email with prepared template files using Asp.net and C#

When bulding professional web applications, there can be so many emails to be sent to users and writing the templates for each of this email inside code is not a good move. Here we can use email templates.
First create a folder some where in your application directory as “MailTemplates” or some other suitable name. Now create a text file or html file for template. In this example we are creating a sample template named welcome.htm.

Now we are creating an email utility for preparing and sending emails.

Now let’s send the email

Cheers

25 thoughts on “How to send email with prepared template files using Asp.net and C#

  1. Anoop M S

    public static class Constants
    {
    //culture info
    public static CultureInfo CurrentCulture
    {
    get
    {
    return CultureInfo.CurrentCulture;
    }
    }
    }

    add this to your code and include using System.Globalization; in namespaces

    Reply
  2. Anoop ms

    body is applied to the whole page, if you want background image for your email, you should probably use <div style='background:<%= bgurl%>'>mail body</div>

    Reply
  3. Anonymous

    Hi Anoop! Great article!! Can you please post the code for
    body = body.Replace("<%={0}%>".FormatWith(pairs[i]), pairs[i + 1]);

    Format.With extension? That is the only line which I can not get to compiles…thanks!! Mike

    Reply
  4. Anoop ms

    public static string FormatWith(this string target, params object[] args)
    {
    return string.Format(Constants.CurrentCulture, target, args);
    }

    Reply
  5. Mike

    Thanks Anoop!
    Do you have a location where the source code for this can be downloaded? This is an excellent example and what I have to do is fairly similar.
    Thanks for your help!!
    MIke

    Reply
  6. Nishant Mohan

    Hi Anoop,

    I Have an aspx page with labels and gridview , I want to email the page as it is. I have tried with stringbuilder, no luck. The catch is without actually opening the page, so I created a stringbuilder to get the html of it.? I am not able to load any aspx control. Help?

    Reply
  7. Anoop ms

    @Nishant
    Hi, you can use htmlwriter for this. Use a asp.net panel after form tag and enclose all the contents in this panel. Then in a server side event such as button click, load this control to a htmlwriter object, then use render function, you will get the rendered html in htmlwriter, rest you know.

    Reply
  8. Anoop ms

    If the panel's id is pnlContainer
    Code:

    System.IO.StringWriter stringWrite = new System.IO.StringWriter();

    System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

    pnlContainer.RenderControl(htmlWrite);

    string html_mail=stringWrite.ToString();

    Hope this helps

    Reply
  9. HarleyDude0524

    Anoop,

    Great article and sample. I am still getting an error in PrepareMailBodyWith at .FormatWith (string does not contain a definition) and in the FormatWith function it doesn't seem to like Constraints (am I just missing a namespace). I am new at C#.

    Reply
  10. HarleyDude0524

    I saw your comment about the constants and that fixed that issue. But U still have a problem with .FormatWith(pairs[i]), pairs[i + 1]); It says, "'Sting' does not have a definition for 'FormatWith' and no extension method 'FormatWith' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference). I am using C# Visual Studio .net 2010 with .net ver 4.0

    Reply
  11. HarleyDude0524

    These are the NameSpaces I am using to try and resolve the FormatWith() issue.

    using System;
    using System.IO;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Net.Mail;
    using System.Net.Mime;
    using System.Web.Configuration;
    using System.Net.Configuration;
    using System.Configuration;
    using System.Globalization;
    using System.String;
    using System.IFormatProvider;
    using System.Object;

    Reply
  12. Anoop ms

    Hi Harley, try adding this to the end of your code

    public static string FormatWith(this string target, params object[] args)
    {
    return string.Format(Constants.CurrentCulture, target, args);
    }

    Reply
  13. Amer Mohammed

    public static string FormatWith(this string target, params object[] args)
    {
    return string.Format(Constants.CurrentCulture, target, args);
    }

    I added this code to the end of my code, but it is still not working…..

    Reply

Leave a Reply to Nishant Mohan Cancel reply

Your email address will not be published. Required fields are marked *