tags:

views:

146

answers:

2

Can I set up HTML/Email Templates in C# on ASP.NET?

This question was asked and answered by SkippyFire and others...I have a follow up question. I like to keep things very simple, as a novice developer.

If I am not correct, Skippyfire said you could send the complete aspx page using this code:

StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
HtmlTextWriter htmlTW = new HtmlTextWriter(sw);
this.Render(htmlTW);

Then just use net.mail to send on Page.Load event. This is very confusing to me. I can use this to render controls to an email.Body and send but I can not use this to load an entire page in anyway I have discovered.

Using Net.mail...

How would I send the page above? I tried to put nothing on the page but some text and send it using it's own page load event... I can not figure out any other way to send it from another page or button... (how would you do this? Wouldn't you have to somehow load the URL into an object?)... anyway I tried to do it from Page Load itself as Skippyfire describes in an old post and get this error from Visual studio IDE:

A page can have only one server-side Form tag.

Any help would be appreciated.

CS

A: 

It would be sometihng like this:

StringBuilder sb = new StringBuilder();
using (StringWriter sw = new StringWriter(sb))
{
    using (HtmlTextWriter htmlTW = new HtmlTextWriter(sw))
    {
        this.Render(htmlTW);
    }

    using (var message = new MailMessage
                         {
                             From = new MailAddress("[email protected]"), 
                             Subject = "This is an HTML Email", 
                             Body = sw.ToString(), 
                             IsBodyHtml = true
                         })
    {
        message.To.Add("[email protected],[email protected]");
        SmtpClient client = new SmtpClient();
        client.Send(message);
    }
}
John Saunders
I think you miss-read his question? Either that or I did :)
csharptest.net
I read the q as being "I've got it rendered; how do I mail it?"
John Saunders
Yea, when I read your response I re-read the question. Even after reading it again I still didn't know who got it right ;)
csharptest.net
A: 

There is another way to do this... You can host the ASP.Net runtime in your application. It's not major difficult either, this is "sort-of" what you need to do...

  1. First step is to create a remote-able object that will be used to communicate with the domain. It's only needed method is one to return the output of a page:

    internal class RemoteAspDomain : MarshalByRefObject
    {
     public string ProcessRequest(string page, string query)
     {
      using (StringWriter sw = new StringWriter())
      {
       SimpleWorkerRequest work = new SimpleWorkerRequest(page, query, sw);
       HttpRuntime.ProcessRequest(work);
       return sw.ToString();
      }
     }
    }
    
  2. Then when your ready to create/work with ASP.Net you setup the environment like:

    public static string RunAspPage(string rootDirectory, string page, string query)
    {
     RemoteAspDomain host;
     try
     {
      host = (RemoteAspDomain)ApplicationHost.CreateApplicationHost(typeof(RemoteAspDomain), "/", rootDirectory);
      return host.ProcessRequest(page, query);
     }
     finally
     {
      ApplicationManager.GetApplicationManager().ShutdownAll();
      System.Web.Hosting.HostingEnvironment.InitiateShutdown();
      host = null;
     }
    }
    
  3. Now you should be able to use it with the following:

    string response = RunAspPage("C:\\MyWebAppRoot\\", "/default.aspx", "asdf=123&xyz=123");
    

Obviously, you don't want to do this for every request as it takes time to perform the startup-shutdown operations. Simply refactor the RunAspPage to be an IDisposable class that destroys the environment on dispose instead of using the finally block.

Update, BTW if Your already running in an ASP.Net session, there are far easier ways to do this. See HttpServerUtility.Execute Method (String, TextWriter)

Please Note: The above code was copy/pasted and simplified from a working copy, I think I got everything you need but my actual implementation is much more complicated. If you have any trouble there are several real-world examples of these API on the internet.

csharptest.net
Csharp, I must say the HTTPSERVER Request utility works incredibly well. You are right I am rendering the data fine I just want to send it formatted in a page. There are only about 1000 different ways to do this with many caveats and some very complicated and long winded solutions out there but this is by far the simplest and fastest... I love it, thanks,CS
CraigJSte