views:

109

answers:

2

I am working on an HTML auto responder email. If there is a simpler way than what i am doing please let me know.

So far, i've built "pagename.aspx" and i read this page into a string variable, then use this variable as the BODY of the mail. This works.

The page optionally accepts a QueryString called "leadID". This is used to pull data from a database and populate fields on this page. This also works fine when i manually browse to the page - pagename.aspx?leadid=xyz

My Problem / Question is, how do i pass this querystring to the page and return the resulting HTML output of the page into a string , which can then be used as the body of my email.

Again, if there is a better way please let me know. I'm using LINQ to SQL, VB.NET and ASP.NET 3.5.

Thanks a million.

+1  A: 

The easiest way is just to do a WebRequest to it:

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();
}
Noon Silk
+2  A: 

Hi Khalid!

As described in this article, you could use the HttpWebRequest class to retrieve the stream of data from your page "pagename.aspx?leadID=1". But that could cause a bit of overhead to your application due to the additional HTTP request.

Wouldn't it be possible/better to generate your HTML content from a simple class? What content generates your page?

Edit: As asked by Khalid here is a simple class to generate a dynamic HTML file using your leadID parameter and a gridview control. It's only an example, you would need to custom it and make more reusable:

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());
    }
}
jdecuyper
Agreed on the overhead; he could always just request the page and hold a token for the data that 'leadID' populates ({leadName} and so on), and then replace it in the calling code, per email.
Noon Silk
This method works great and since this specific autoresponder will only be used about 10 times a day if so much it won't add too much overhead for now. However, in the interest of optimization can you please elaborate on your statement "Wouldn't it be possible/better to generate your HTML content from a simple class?"
Khalid Rahaman
Hi Khalid! I edited my post, let me know if this helps!
jdecuyper