简体   繁体   English

如何将aspx页面作为电子邮件模板发送

[英]How to Send an aspx page as an email Template

Can I set up HTML/Email Templates in C# on ASP.NET? 我可以在ASP.NET上的C#中设置HTML /电子邮件模板吗?

This question was asked and answered by SkippyFire and others...I have a follow up question. SkippyFire等人提出并回答了这个问题。我有一个后续问题。 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: 如果我不正确,Skippyfire说您可以使用以下代码发送完整的aspx页面:

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. 然后只需使用net.mail发送Page.Load事件。 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... 使用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: 我试图在页面上只放一些文本,然后使用自己的页面加载事件发送它……我无法找出从其他页面或按钮发送它的任何其他方式……(您将如何做? “您不必以某种方式将URL加载到对象中吗?)...无论如何,我尝试从Skippyfire在旧帖子中描述的页面加载本身来做到这一点,并从Visual Studio IDE中获取此错误:

A page can have only one server-side Form tag. 一个页面只能有一个服务器端Form标记。

Any help would be appreciated. 任何帮助,将不胜感激。

CS 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. 还有另一种方法可以...您可以在应用程序中托管ASP.Net运行时。 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: 然后,当您准备好使用ASP.Net创建/使用ASP.Net时,可以像下面这样设置环境:

     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. 只需将RunAspPage重构为一个IDisposable类,即可销毁处置环境,而不是使用finally块。

Update, BTW if Your already running in an ASP.Net session, there are far easier ways to do this. 更新,顺便说一句,如果您已经在ASP.Net会话中运行,则有更简单的方法来执行此操作。 See HttpServerUtility.Execute Method (String, TextWriter) 请参见HttpServerUtility.Execute方法(字符串,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. 如果您有任何麻烦,可以在Internet上找到这些API的一些实际示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM