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

Posted on 2009-02-14
Medium Priority
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
  • 3
  • 2
LVL 15

Accepted Solution

aibusinesssolutions earned 2000 total points
ID: 23642147
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

ID: 23642208
yea...knew it was gonna wind up being simple.  thanks

Author Comment

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

Expert Comment

ID: 23642294
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

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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Loops Section Overview
Suggested Courses

850 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