render HTML output to an email?

  • Thread starter Thread starter Tarren
  • Start date Start date
T

Tarren

Hi:

I have an aspx page that takes a dataset, makes the datagrid, and displays
it.

The new thing I have to do is take this same output and send it as an email.

Is there an easy way to do this? Any examples?

Thanks for the help!

- T
 
use a webclient and open the webpage and read it into a stream. get the data
and use it as a body.
from an old piece of code.

WebClient wbTellAFriend = new WebClient();
NameValueCollection myQS = new NameValueCollection();
myQS.Add("ProductID", Convert.ToString(ProductID));
myQS.Add("ProductColorID", Convert.ToString(ProductColorID));
wbTellAFriend.QueryString = myQS;

bool localBuild =
bool.Parse(ConfigurationSettings.AppSettings["localBuild"]);
string appPath = (localBuild == true ?
ConfigurationSettings.AppSettings["localPath"] :
ConfigurationSettings.AppSettings["onlinePath"]);
Stream sData = wbTellAFriend.OpenRead(appPath +
"Customers/Home/WCTellAFriend.aspx");
StreamReader sDataReader = new StreamReader(sData);
string mailBody = sDataReader.ReadToEnd();
mailBody = mailBody.Replace("MemberName", txtYourName.Text.Trim() + "(" +
txtYourEmail.Text.Trim() + ")");

--

Regards,

Hermit Dave
(http://hdave.blogspot.com)
 
Hi,

here is an example.

Assume you have ASPX as follows:
***************************
<form id="Form1" method="post" runat="server">
<asp:DataGrid id="DataGrid1" runat="server"></asp:DataGrid>
<asp:Button ID="btnSend" Runat="server" Text="Send as email" />
<asp:Label ID="lblInfo" Runat="server" />
</form>
***************************
And code as follows on page:
***************************
Private Sub BindGrid()
'Create an example data source
Dim dt As New DataTable
dt.Columns.Add("ID", GetType(System.Int32))
dt.Columns("ID").AutoIncrement = True
dt.Columns.Add("Text", GetType(System.String))

Dim dr As DataRow = dt.NewRow()
dr(1) = "First"
dt.Rows.Add(dr)

dr = dt.NewRow()
dr(1) = "Second"
dt.Rows.Add(dr)

dr = dt.NewRow()
dr(1) = "Third"
dt.Rows.Add(dr)

'Bind the dataGrid
DataGrid1.DataSource = dt
DataGrid1.DataBind()

End Sub

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here

If Not Page.IsPostBack Then
BindGrid()
End If

'If email was sent
If Request.QueryString("emailsent") = "true" Then
lblInfo.Text = "Email was sent"
End If
End Sub

Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnSend.Click
'Set flag to indicate we want to send an email
Context.Items("sendasemail") = True
End Sub


Protected Overrides Sub Render(ByVal writer As
System.Web.UI.HtmlTextWriter)

'Rendering logic varies based on if rendering or sending the email
If Context.Items("sendasemail") Is Nothing Then
MyBase.Render(writer)
Else

'Get the HTML output
Dim htmlStringWriter As New IO.StringWriter
Dim customHtmlWriter As New HtmlTextWriter(htmlStringWriter)

'We are only interested in the DataGrid's output
DataGrid1.RenderControl(customHtmlWriter)

'If you want to get the complete page output
'Use MyBase.Render(customHtmlWriter)

'Send the email
Dim mailmsg As New Mail.MailMessage
mailmsg.To = "(e-mail address removed)"
mailmsg.From = "(e-mail address removed)"
mailmsg.Subject = "Page you requested"

mailmsg.Body = htmlStringWriter.ToString()
mailmsg.BodyFormat = Mail.MailFormat.Html
System.Web.Mail.SmtpMail.SmtpServer = "yoursmtpserver"
System.Web.Mail.SmtpMail.Send(mailmsg)
Response.Redirect(Request.Url.AbsoluteUri & "?emailsent=true")


End If
End Sub
***************************************

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist
http://blogs.aspadvice.com/joteke
 
Tarren said:
Hi:

I have an aspx page that takes a dataset, makes the datagrid, and displays
it.

The new thing I have to do is take this same output and send it as an email.

Is there an easy way to do this? Any examples?

Thanks for the help!

- T
If you need some structure (and it is just data) your can try something like
this.
(ds is your dataset)
dim stw as new stringwriter
ds.writexml(stw, XmlWriteMode.IgnoreSchema )
stw.flush
youremailcode(stw.tostring())
 
Tarren said:
Hi:

I have an aspx page that takes a dataset, makes the datagrid, and displays
it.

The new thing I have to do is take this same output and send it as an email.

Is there an easy way to do this? Any examples?

Thanks for the help!

- T
Sorry I misread your post. This might help you.


Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)

Dim _stringBuilder As StringBuilder = New StringBuilder()
Dim _stringWriter As StringWriter = New StringWriter(_stringBuilder)
Dim _htmlWriter As HtmlTextWriter = New HtmlTextWriter(_stringWriter)
MyBase.Render(_htmlWriter)
Dim html As String = _stringBuilder.ToString()
The 'html' string variable contains the html text write it out to email
here.
This writes it to the browser.
writer.Write(html)

End Sub
 
Back
Top