posted on Wednesday, January 05, 2005 8:40 PM by NenoLoje

Emailing the Rendered Output of an ASP.NET Web Control

Extracted and converted original source by Scott Mitchell.

// You can render any ASP.NET control to pure HTML like this:

StringBuilder sb = new StringBuilder();

StringWriter sw = new StringWriter(sb);

HtmlTextWriter htmlTW = new HtmlTextWriter(sw);

cControlToRender.RenderControl(htmlTW);

string html = sb.ToString();

// And use the output as the body of an HTML mail.

using System.Web.Mail;

...

MailMessage msg = new MailMessage();

msg.To = "someone@dotnet-online.com";

msg.From = "whoever@dotnet-online.com";

msg.Subject = "ASP.NET Rendered HTML Mail";

msg.BodyFormat = MailFormat.Html;

msg.Body = html;

...

SmtpMail.Send(msg);

 

Comments