2009-10-02 1 views
0

HTML 자동 응답 전자 메일을 작성 중입니다. 내가하고있는 것보다 간단한 방법이 있다면 알려주십시오.웹 페이지에서 HTML을 읽고 HTML 자동 응답 전자 메일의 본문으로 사용하는 방법

지금까지 "pagename.aspx"를 빌드했으며이 페이지를 문자열 변수로 읽은 다음이 변수를 메일의 BODY로 사용합니다. 이 작동합니다.

이 페이지는 선택적으로 "leadID"라는 QueryString을 허용합니다. 데이터베이스에서 데이터를 가져 와서이 페이지의 필드를 채우는 데 사용됩니다. ? 내가 수동으로 페이지를 탐색 할 때이 또한 잘 작동 - pagename.aspx leadid = XYZ

내 문제/질문 내가 페이지에이 쿼리 문자열을 전달하고, 페이지의 인쇄 결과 HTML 출력을 반환 어떻게,입니다 문자열은 내 이메일의 본문으로 사용할 수 있습니다.

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

감사합니다. ? 당신이 당신의 페이지 "pagename.aspx leadID = 1에서 데이터의 스트림을 검색 할 수 HttpWebRequest 클래스를 사용할 수,

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(); 
} 
+0

쿼리 문자열을 스크립트 나 출력 페이지에 어떻게 전달합니까? – Anthony

+0

쿼리 문자열을 페이지에 전달한 후 렌더링되는 HTML을 어떻게 읽습니까? –

+0

메모리에서이 페이지의 새 인스턴스를 만들고 변수를 전달하고 출력을 읽어야한다고 생각하지만이 작업을 수행하는 방법을 모르겠습니다. –

답변

3

가장 쉬운 방법은 그것에 WebRequest을 할 그냥 ". 하지만 추가 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()); 
    } 
} 
2

바와 같이 in this article 설명 :

+0

오버 헤드에 동의; 그는 항상 페이지를 요청하고 'leadID'가 채워지는 데이터 ({leadName} 등)에 대한 토큰을 보유한 다음 이메일 당 호출 코드에서이를 바꿀 수 있습니다. –

+0

이 방법은 매우 효과적이며, 너무 많은 오버 헤드를 추가하지 않으면이 특정 자동 응답은 하루에 약 10 회만 사용됩니다. 그러나 최적화를 위해 "간단한 클래스에서 HTML 콘텐츠를 생성하는 것이 가능하지 않습니까?"라는 내용을 자세히 설명해주십시오. –