웹 페이지에서 HTML을 읽고 HTML 자동 응답자 전자 메일의 본문으로 사용하는 방법

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

  •  19-09-2019
  •  | 
  •  

문제

HTML Auto Responder 이메일을 작업 중입니다. 내가하고있는 것보다 더 간단한 방법이 있다면 알려주세요.

지금까지 "pagename.aspx"를 만들었고이 페이지를 문자열 변수로 읽은 다음이 변수를 메일 본문으로 사용합니다. 이것은 작동합니다.

이 페이지는 선택적으로 "leadid"라는 쿼리 스트링을 수락합니다. 이것은 데이터베이스에서 데이터를 가져 와서이 페이지의 필드를 채우는 데 사용됩니다. 페이지를 수동으로 탐색 할 때도 잘 작동합니다 -agegename.aspx? leadid = xyz

내 문제 / 질문입니다,이 queryString을 페이지로 전달하고 페이지의 결과 HTML 출력을 문자열로 반환 한 다음 내 이메일 본문으로 사용할 수 있습니다.

다시, 더 나은 방법이 있다면 알려주세요. LINQ에서 SQL, VB.NET 및 ASP.NET 3.5를 사용하고 있습니다.

대단히 감사합니다.

도움이 되었습니까?

해결책

가장 쉬운 방법은 단지 a를하는 것입니다 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 컨텐츠를 생성하는 것이 가능하지 않습니까? 페이지를 생성하는 콘텐츠는 무엇입니까?

편집 : 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());
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top