如何阅读HTML出一个网页,并用它作为HTML自动应答电子邮件的身体
-
19-09-2019 - |
题
我的工作在一个HTML自动回复电子邮件。如果有一个更简单的方法比我在做什么,请让我知道。
到目前为止,我已经建立了“pagename.aspx”我阅读此页为一个字符串变量,然后使用这个变量作为邮件的正文。其工作原理。
在页面可选地接受一个名为“leadID”查询字符串。这是用来从数据库中提取数据和填充字段此页面上。这也能正常工作时,我手动浏览页面 - ?pagename.aspx leadid = XYZ
我的问题/问题是的,我怎么这个查询字符串传递给页面和页面生成的HTML输出返回到一个字符串,然后它可以作为我的电子邮件的正文。
此外,如果有更好的方法,请让我知道。我使用LINQ到SQL,VB.NET和ASP.NET 3.5。
由于一百万。
解决方案
在最简单的方法是只是做一个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());
}
}
不隶属于 StackOverflow