3

Possible Duplicate:
Razor views as email templates

I am sending out an email to the user from within my service of my website.

I want to format this so that it shows nice and a specific way.

Here is what I have:

<table bgcolor="#FFE680" border="0" cellpadding="0" cellspacing="0" width="100%">
  <tbody>
    <tr>
      <td width="100%" height="51" valign="middle"><h1> <strong>[COMPANY] User ID Reminder </strong></h1></td>
      <td align="right" valign="top" width="8"><img alt="" width="8" height="8" align="top" /></td>
    </tr>

  </tbody>
</table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
  <tbody>
    <tr>
      <td><table border="0" cellpadding="0" cellspacing="0" width="100%">
        <tbody>
          <tr>
            <td align="left" valign="top" width="100%"><table border="0" cellpadding="0" cellspacing="0" width="100%">
              <tbody>
                <tr>
                  <td><table border="0" cellpadding="0" cellspacing="0" width="100%">
                    <tbody>
                      <tr>
                        <td colspan="1" align="left"></td>
                        </tr>
                      <tr>
                        <td align="left" valign="top" width="100%"><p> <br />
                          Dear [USERNAME], </p>
                          <p> In response to your request to be reminded of your User ID, please   find below the information we have on file for you. If you didn't   submit  this request, ignore this email. </p>
                          <table border="0" cellpadding="0" cellspacing="0">
                            <tbody>
                                  <tr>
                                    <td>Your User ID is: </td>
                                    <td>[USERNAME]</td>
                                    </tr>
                                  <tr>
                                    <td>Your registered email address is:  </td>
                                    <td>[EMAIL]</td>
                                  </tr>
                              </tbody>
                            </table>
                          <p> 
                            If you have forgotten your password, you can request it here.<br />
                          </p>
                          <p> 
                            Thank you,<br />[COMPANY]
                          </p></td>
                        </tr>
                      </tbody>
                    </table></td>
                  </tr>
                </tbody>
              </table></td>
          </tr>
          </tbody>
      </table></td>
    </tr>
  </tbody>
</table>

Now my question isn't about the looks of the email but how do I put this as the body of my email while populating the appropriate fields?

I put it in resources as a string and I figured I'd just do a replace of the specific [fields]. This starting becoming tedious and seemed sloppy:

var body = SuburbanHUB.Properties.Resources.ForgotPasswordEmailBody.Replace("[USERNAME]", username).Replace("[EMAIL]")...

I'm sure there is a better way of doing this but I have not the experience.

=== CLARIFICATION ===

I am using razor to call a WCF service written in C#. The service that is being called is where the email is being sent from and not the view. I am using Razor with MVC with C# as the underlying code.

Community
  • 1
  • 1
ErocM
  • 4,505
  • 24
  • 94
  • 161
  • You don't say if you're sending this from a website, an WinForms program, an WPF one, or otherwise. It can make a big difference to what answers are available to you. – Bobson Oct 29 '12 at 20:33
  • What is a "C# service"? A web service? WCF Service? – jrummell Oct 29 '12 at 20:43
  • 2
    we are using WCF Service. Jeez man, learn to read my mind!! :P – ErocM Oct 29 '12 at 20:43
  • just as a comment, I'm using the exact same approach but instead of [USERNAME] I'm just using {i} and the string.format function. – sebagomez Nov 02 '12 at 14:47

2 Answers2

1

I had similar requirement and I end up using NVelocity and it worked great for me. Please see this article for a workthrough http://www.codeproject.com/Articles/12751/Template-merging-with-NVelocity-and-ASP-NET. You have to modify your fields in the template according to the velocity templating language. Download NVelocity from http://nvelocity.sourceforge.net/.

Tariqulazam
  • 4,535
  • 1
  • 34
  • 42
  • I like this option, but it wouldn't have worked for me. We needed to embed logic into our templates, and embed other templates into them. This is great for anything simple though, which includes the OP's example. Definitely an easier solution than mine. – Bobson Oct 29 '12 at 20:39
  • As I know, velocity provides nested templating functionality and some conditions to be included in the template itself, but I never tried it. You may want to check the velocity template reference guide for this. http://velocity.apache.org/engine/releases/velocity-1.5/vtl-reference-guide.html – Tariqulazam Oct 29 '12 at 20:51
1

I'm in the middle of trying to do the same thing. We're using Razor templates to generate the email, and passing in a Model which has all the various variables to fill it out. This lets us include everything that Razor and MVC support, including @if and @Html.Partial, which lets us construct an email from pieces.

The answer we just went with involves running an internal MVC webserver, requesting pages from it with the Model as a parameter, and capturing the response text, but there's variants on my question that are more self contained. Take a look and see if any of the other answers or comments help you.

Community
  • 1
  • 1
Bobson
  • 13,498
  • 5
  • 55
  • 80