0

我正在处理 HTML 自动回复电子邮件。如果有比我正在做的更简单的方法,请告诉我。

到目前为止,我已经构建了“pagename.aspx”并将这个页面读入一个字符串变量,然后使用这个变量作为邮件的正文。这行得通。

该页面可选择接受名为“leadID”的 QueryString。这用于从数据库中提取数据并填充此页面上的字段。当我手动浏览到页面时,这也可以正常工作 - pagename.aspx?leadid=xyz

我的问题/问题是,我如何将此查询字符串传递给页面并将页面的结果 HTML 输出返回为字符串,然后可以将其用作我的电子邮件的正文。

再次,如果有更好的方法,请告诉我。我正在使用 LINQ to SQL、VB.NET 和 ASP.NET 3.5。

太感谢了。

4

2 回答 2

3

最简单的方法就是WebRequest对它做一个:

string url = "...";
string result;

HttpWebRequest webrequest = (HttpWebRequest) HttpWebRequest.Create(url);
webrequest.Method        = "GET";
webrequest.ContentLength = 0;

WebResponse response = webrequest.GetResponse();

using(StreamReader stream = new StreamReader(response.GetResponseStream())){
    result = stream.ReadToEnd();
}
于 2009-10-02T02:17:22.483 回答
2

本文所述,您可以使用 HttpWebRequest 类从您的页面“pagename.aspx?leadID=1”中检索数据流。但是由于额外的 HTTP 请求,这可能会给您的应用程序带来一些开销。

从一个简单的类生成 HTML 内容不是可能/更好吗?什么内容生成您的页面?

编辑:正如 Khalid 所问的,这里有一个简单的类,可以使用您的 LeadID 参数和 gridview 控件生成动态 HTML 文件。这只是一个示例,您需要对其进行自定义并使其更可重用:

using System;
using System.Text;
using System.IO;
using System.Web.UI.WebControls;
using System.Web.UI;

public class PageBroker
{

    /*
     * How to use PageBroker:
     * 
     *  string leadID = "xyz"; // dynamic querystring parameter
     *  string pathToHTML = Server.MapPath(".") + "\\HTML\\leadForm.html"; //more detail about this file below
     *  PageBroker pLeadBroker = new PageBroker(pathToHTML, leadID);  
     *  Response.Write(pLeadBroker.GenerateFromFile()); // will show content of generated page
     */

    private string _pathToFile;
    private StringBuilder _fileContent;
    private string _leadID;

    public PageBroker(string pathToFile, string leadID)
    {
        _fileContent = new StringBuilder();
        _pathToFile = pathToFile;
        _leadID = leadID;
    }

    public string GenerateFromFile() {
        return LoadFile();
    }
    private string LoadFile()
    {
        // Grab file and load content inside '_fileContent'
        // I used an html file to create the basic structure of your page
        // but you can also create
        // a page from scratch.
        if (File.Exists(_pathToFile))
        {
            FileStream stream = new FileStream(_pathToFile, FileMode.Open, FileAccess.Read);
            StreamReader reader = new StreamReader(stream);
            while (reader.Peek() > -1)
                _fileContent.Append(reader.ReadLine() + "\n");
            stream.Close();
            reader.Close();

            InjectTextContent();
            InjectControlsContent();
        }        
        return _fileContent.ToString();
    }

    // (Ugly) method to inject dynamic values inside the generated html
    // You html files need to contain all the necesary tags to place your
    // content (for example: '__USER_NAME__')
    // It would be more OO if a dictionnary object was passed to the 
    // constructor of this class and then used inside this method 
    // (I leave it hard-coded for the sake of understanding but 
    // I can give you a more detailled code if you need it).
    private void InjectTextContent() {
        _fileContent.Replace("__USER_NAME__", "Khalid Rahaman");
    }

    // This method add the render output of the controls you need to place
    // on the page. At this point you will make use of your leadID parameter,
    // I used a simple array with fake data to fill the gridview.
    // You can add whatever control you need.
    private void InjectControlsContent() {
        string[] products = { "A", "B", "C", "D" };
        GridView gvProducts = new GridView();
        gvProducts.DataSource = products;
        gvProducts.DataBind();

        // HtmlTextWriter is used to write HTML programmatically. 
        // It provides formatting capabilities that ASP.NET server 
        // controls use when rendering markup to clients 
        // (http://msdn.microsoft.com/en- us/library/system.web.ui.htmltextwriter.aspx)
        // This way you will be able to inject the griview's 
        // render output inside your file.
        StringWriter gvProductsWriter = new StringWriter();
        HtmlTextWriter htmlWrit = new HtmlTextWriter(gvProductsWriter);
        gvProducts.RenderControl(htmlWrit);
        _fileContent.Replace("__GRIDVIEW_PRODUCTS__", gvProductsWriter.ToString());
    }
}
于 2009-10-02T02:22:51.433 回答