日历归档 |
|
<< < 2024 - 11 > >> | Su | Mo | Tu | We | Th | Fr | Sa | | | | | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 |
|
|
|
About Me |
|
|
ZhangSichu |
|
Male |
|
32 |
|
ZhangSichu@gmail.com |
|
ZhangSichu@hotmail.com |
|
ZhangSichu.com |
|
weibo.com/zhangsichu |
|
|
|
个人推荐 |
|
|
|
|
分类归档 |
|
|
|
|
My Friends |
|
|
|
|
截取一个Asp.Net页面中的部分内容 作为邮件的一部分发送给用户
|
截取一个Asp.Net页面中的部分内容 作为邮件的一部分发送给用户
问题描述: 下面是一个Asp.Net的page
****************************************************** *Header ****************************************************** *Body * * * * * ****************************************************** *Footer * Send[Button] ****************************************************** 当用户点击Send后 抽取body 部分的html内容当作邮件正文的一部分发给用户。 解法1: 在Send Button Click后,先执行页面中JavaScript把Body中的内容存到一个hidden filed中,然后PostBack. Client script. <script type="text/javascript"> function ClientGet() { //result is a div. var result = document.getElementById("result"); var hiddenInfo = document.getElementById("hiddenInfo"); //alert(result.innerText); hiddenInfo.value = result.innerHTML; } </script> Server button. <asp:Button ID="btnSendMail" runat="server" Text="Send" OnClientClick="ClientGet();" OnClick="btnSendMail_Click"/> private void SendMail( string body) { SmtpClient smtpClient = new SmtpClient(); MailMessage message = new MailMessage(); MailAddress fromAddress = new MailAddress("zhangsichu@gmail.com", "HtmlPartMail"); smtpClient.Host = "MailEXG"; //smtpClient.Port = 25; message.From = fromAddress; message.To.Add("zhangsichu@gmail.com"); message.Subject = "HtmlPartMail"; message.IsBodyHtml = true; message.Body = body; smtpClient.Send(message); } protected void btnSendMail_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(this.hiddenInfo.Value)) this.SendMail(this.hiddenInfo.Value); } <pages enableEventValidation="false" validateRequest="false"/>
解法2: override page 的 Render 方法。当页面Render前用一个static的StringBuider 记住全部html protected override void Render(HtmlTextWriter writer) { HtmlForMailPart = new StringBuilder(); StringWriter stringWriter = new StringWriter(HtmlForMailPart); HtmlTextWriter innerWriter = new HtmlTextWriter(stringWriter); base.Render(innerWriter); innerWriter.Close(); writer.Write(HtmlForMailPart.ToString()); //base.Render(writer); } 取得Body部分 public const string HtmlForMailPartBeginKey = "<!-- mial html part begin -->"; public const string HtmlForMailPartEndKey = "<!-- mial html part end -->"; public string GetMailHtmlPart() { if (HtmlForMailPart == null) { return string.Empty; }
string htmlContent = HtmlForMailPart.ToString(); int startIndex = htmlContent.IndexOf(HtmlForMailPartBeginKey); int endIndex = htmlContent.IndexOf(HtmlForMailPartEndKey);
if (endIndex < startIndex) { return string.Empty; } htmlContent = "<table><head><style>" + " td { font-size: 12px }" + " body { font-size: 12px }" + "</style></head><body>" + htmlContent.Substring(startIndex, endIndex - startIndex) + "</body></html>";
return htmlContent; } HtmlForMailPartBeginKey HtmlForMailPartEndKey 这两个tag放在要取得内容的头和尾。
|
|
#re:截取一个Asp.Net页面中的部分内容 作为邮件的一部分发送给用户 10/25/2006 9:39:04 PM bill
|
我想,通过JavaScript也能解决这个问题吧 运用一些正则表达式,能完成一些更复杂点的需求,如:抓取页面中任何条件内容
|
|
|
|
|
|