Webページのうち、HTMLを読み、HTML自動レスポンダEメールの本文としてそれを使用する方法

StackOverflow https://stackoverflow.com/questions/1507351

  •  19-09-2019
  •  | 
  •  

質問

私は、HTML自動応答メールに取り組んでいます。私がやっているものよりも簡単な方法がある場合は私に知らせてくださいます。

これまでのところ、私は「pagename.aspx」を構築したと私はその後、メールの本文として、この変数を使用して、文字列変数に、このページを読んで。これは動作します。

ページは、必要に応じて「leadID」と呼ばれるのQueryStringを受け入れます。これは、データベースからデータを取得し、このページのフィールドを埋めるために使用されています。私は手動でページを参照するとき、これはまた、正常に動作します - ?pagename.aspx leadid = XYZ

の私の問題/質問は、どのように私はページに、このクエリ文字列を渡し、その後、私の電子メールの本文として使用できる文字列へページの結果のHTML出力を返すか、のです。

ここでも、より良い方法があれば私に知らせてください。私は、SQL、VB.NETおよびASP.NET 3.5にLINQを使用しています。

おかげで百万ます。

役に立ちましたか?

解決

最も簡単な方法はそれに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();
}

他のヒント

この記事の中で説明したように、

、あなたはHttpWebRequestのを使用することができますあなたのページからのデータのストリームを取得するためのクラス「pagename.aspx?leadID = 1」。しかし、それは追加のHTTP要求のために、アプリケーションにオーバーヘッドのビットを引き起こす可能性があります。

単純なクラスから自分のHTMLコンテンツを生成するために、より良い/ことはできないでしょうか?どのようなコンテンツがあなたのページを生成する?

編集: ここではハリドで尋ねたとして、あなたの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());
    }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top