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
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("from@company.com"),
Subject = "This is an HTML Email",
Body = sw.ToString(),
IsBodyHtml = true
})
{
message.To.Add("toaddress1@company.com,toaddress2@company.com");
SmtpClient client = new SmtpClient();
client.Send(message);
}
}
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:
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(); } } }
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; } }
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.
精彩评论