System.Net.Mail.MailMessage:  Insert Body from File

Posted on 2009-02-14
Last Modified: 2012-05-11
The body of my email is fairly complicated HTML.  instead of trying to code it all in the code behind file, I'd like to code it in an html file (possibly called BodyOfMessage.htm or BodyOfMessage.txt) and then read/insert the file in as the body of the email.

How do i do this?
Question by:davidcahan
    LVL 15

    Accepted Solution

    That's pretty simple, you just open the HTML file as a string, and then replace the variables.

    Dim emailFile As String = IO.File.ReadAllText(Server.MapPath("~/path-to-file/BodyOfMessage.htm"))
    emailFile.Replace("[Variable]", variable)

    EmailMsg.Body = emailFile

    Author Comment

    yea...knew it was gonna wind up being simple.  thanks

    Author Comment

    actually...what is that part where you replace the variables?  what variables are you replacing
    LVL 15

    Expert Comment

    In your HTML file, you could have something like this

    <p>Dear [Fullname],</p>
    <p>Thank you for ordering [productName].</p>

    Then you would replace them like template fields.
    emailFile.Replace("[FullName]", obj.FullName)
    emailFile.Replace("[productName]", obj.ProductName)

    Author Comment

    ahhh...ok. that's what i thought but i wasn't sure.  thanks again

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
    IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now