There can be no Triumph without Loss,No Victory without Suffering,No Freedom without Sacrifice.
All you have to decide is what to do with the time that is given to you.
Get busy Living, or Get busy Dying?
  首页 | 留言给我 | 订阅 Rss | CLI | 黄白之恋 Posts:158   Hits: 2066425    Comments: 170    
 日历归档
<<  <  2020 - 04  >  >>
SuMoTuWeThFrSa
   1234
567891011
12131415161718
19202122232425
2627282930
 About Me
 Name: ZhangSichu
 Sex: Male
 Age: 32
 Email: ZhangSichu@gmail.com
 MSN: ZhangSichu@hotmail.com
 Home: ZhangSichu.com
 WeiBo: weibo.com/zhangsichu
 个人推荐
 分类归档
  ·C++/C(5)  RSS
  ·软件工程(1)  RSS
  ·杂事/随感(26)  RSS
  ·.Net/Java(30)  RSS
  ·面向对象程序设计(5)  RSS
  ·汇编/破解(0)  RSS
  ·平面设计(3)  RSS
  ·SQL(5)  RSS
  ·COM/COM+(2)  RSS
  ·Web开发(81)  RSS
 My Friends
Back Forward Refresh Home 2020年4月5日 星期日 RSS CLI Mine Sweeper. In Javascript.

  截取一个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放在要取得内容的头和尾。
  Posted @ 9/1/2006 12:03:57 AM | Hits (72300) | Comments (1

  Comment
 #re:截取一个Asp.Net页面中的部分内容 作为邮件的一部分发送给用户  10/25/2006 9:39:04 PM  bill
我想,通过JavaScript也能解决这个问题吧
运用一些正则表达式,能完成一些更复杂点的需求,如:抓取页面中任何条件内容
  Post Comment
标题 *
作者 *
密码 记住我
评论 *
    


Stable in Firefox 1.5 2.0Stable in IE6 IE7Stable in MozillaStable in Netscape
ZhangSichu.com V0.1.7507
Powered By ZhangSichu
Copyright © ZhangSichu
Download ZhangSichu.com source code. Download source code