Hi I'm having a bit of a nightmare here!
I'mv trying output a webform to html using page.rendercontrol and htmltextwriter but it is resulting in a blank email.
Code:
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
HtmlTextWriter htmlTW = new HtmlTextWriter(sw);
page.RenderControl(htmlTW);
String content = sb.ToString();
MailMessage mail = new MailMessage();
mail.From = new MailAddress("[email protected]");
mail.To.Add("[email protected]");
mail.IsBodyHtml = true;
mail.Subject = "Test";
mail.Body = content;
SmtpClient smtp = new SmtpClient("1111");
smtp.Send(mail);
Response.Write("Message Sent");
I also tried it by rendering a single textbox and got and error saying It needed to be within form tags (which are on the masterpage).
I tried this fix: http://forums.asp.net/p/1016960/1368933.aspx#1368933 and added:
public override void
VerifyRenderingInServerForm(Control control) { return; }
But now the errror I get is:
VerifyRenderingInServerForm(Control)': no suitable method found to override
Does anyone have a fix for this? I'm tearing my hair out!!
Thanks,
Steve
If you don't need to redirect the page, after you render the contents of the page (from your code sample, it doesn't look like you need to), then you may want to use a Response.Filter.
Off the top of my head, it would look something like:
The SmtpFilter class, is just a class that inherits from the Stream object.
The main method will be the Write method. Here is some code off the top of my head to Override the Write(...) method, send the Smtp mail, and continue on processing.
If you need more help on Response.Filters, you may want to google it. The first article I found was in VB.NET, but still helpful:
http://aspnetlibrary.com/articledetails.aspx?article=Use-Response.Filter-to-intercept-your-HTML