For Programmers: Free Programming Magazines  


Home > Archive > ASP .NET > August 2004 > render HTML output to an email?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author render HTML output to an email?
Tarren

2004-08-30, 3:59 pm

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


Curt_C [MVP]

2004-08-30, 3:59 pm

write it all to a string and write it to the e-mail object

--
Curt Christianson
Owner/Lead Developer, DF-Software
Site: http://www.Darkfalz.com
Blog: http://blog.Darkfalz.com


"Tarren" <noemailplease@thankyou> wrote in message
news:uaa2x0rjEHA.1764@TK2MSFTNGP10.phx.gbl...
> 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
>



Hermit Dave

2004-08-30, 3:59 pm

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)
"Tarren" <noemailplease@thankyou> wrote in message
news:uaa2x0rjEHA.1764@TK2MSFTNGP10.phx.gbl...
> 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
>
>



Teemu Keiski

2004-08-30, 3:59 pm

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 = "receiver@domain.com"
mailmsg.From = "sender@domain.com"
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" <noemailplease@thankyou> wrote in message
news:uaa2x0rjEHA.1764@TK2MSFTNGP10.phx.gbl...
> 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
>
>



Tarren

2004-08-30, 8:57 pm

thanks all!
"Tarren" <noemailplease@thankyou> wrote in message
news:uaa2x0rjEHA.1764@TK2MSFTNGP10.phx.gbl...
> 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
>



vMike

2004-08-30, 8:57 pm


"Tarren" <noemailplease@thankyou> wrote in message
news:uaa2x0rjEHA.1764@TK2MSFTNGP10.phx.gbl...
> 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())


vMike

2004-08-30, 8:57 pm


"Tarren" <noemailplease@thankyou> wrote in message
news:uaa2x0rjEHA.1764@TK2MSFTNGP10.phx.gbl...
> 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


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2010 codecomments.com