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
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…
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:
Then when your ready to create/work with ASP.Net you setup the environment like:
Now you should be able to use it with the following:
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.