tags:

views:

483

answers:

5

Hello,

After user places an order I have to send detailed email message containing order details, instructions for payment, some additional text etc.

I want to create nicely formatted HTML email message.

By now I found two options:

  • manually creating piece by piece, string by string, which is too cumbersome,
  • creating actual aspx page and binding data, then rendering that page as html and sending as body of email.

This second option is more visual and easier to implement except:

  • I do not know how to actually load and render page, I know how to do it with ascx
  • This seems to much of overhead to instantiate page and render it

How to load page and render it? Do you have any other ideas or suggestions for this task?

+2  A: 

Personally, I'd want to use a template, either in a database, or as a file that gets loaded. This template would have most of the content for the email in HTML, with tokens that I can replace with the content.

ex.

<b>Receipt for order # [[ordernum]]</b>

That way I could use simple string replacement to place the dymanic content into the email, without having to build the whole email every time it needs to be sent.

Jay S
No doubt that would work and I have used this method for simple emails, but in cases where data to be replaced is quite dynamic or retrieved from a database, it might be better to use an ASPX resource itself.
Cerebrus
How do you bind DataTable to template? Iterate row by row and create required html structure?
Milan
+3  A: 

Well, IMO, your basic problem amounts to "How do I convert an ASPX resource into an HTML string to pass to the MailMessage Body property ?"

You could do that simply by using a WebRequest to the ASPX URL in question and read that response into a Stream. Then simply read the stream into a string and your primary problem is solved.

Edit: Here's an article that illustrates this concept.

Cerebrus
A: 

Try using a template stored in a .NET string resource file. Down the line this will make localization a lot easier too.

Mike Robinson
+1  A: 

In a similar situation I store a template email message in my database so that the people who use our software can modify the message. It is created (by the user) using the online HTML editor control from Telerik. Within this message, I support several "mailmerge" type fields that all have the pattern {FirstName}, {LastName}, etc.

When it is time to send the message, I pull the formatted text from the database, use string replace to fill in any slots in the template, and then send it. I guess the key is that I know the message is HTML formatted because the Telerik control helps ensure that it is so. However, there is no reason why you couldn't create your HTML and then just save it for later use.

The .aspx page route? I just wouldn't do it. It is way overkill and doesn't offer you any advantages.

Mark Brittingham
A: 

I'll use a template like Jay mentioned.

Below resource might turn out useful for you.

http://dotnettricks.com/blogs/roberhinojosablog/archive/2006/05/12/57.aspx